Last active
October 4, 2023 18:33
-
-
Save AryanGitHub/edcca3774cda6563fca272f80a109951 to your computer and use it in GitHub Desktop.
Computer Network Lab Programs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.net.*; | |
import java.io.*; | |
public class lab5DayTimeClient{ | |
public static void main (String args[]) throws Exception { | |
Socket soc = new Socket("localhost" , 1300); | |
InputStreamReader r = new InputStreamReader (soc.getInputStream()); | |
BufferedReader br = new BufferedReader (r); | |
System.out.println ("Server: "+br.readLine()); | |
br.close(); | |
r.close(); | |
soc.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.net.*; | |
import java.io.*; | |
public class lab5DaytimeServer{ | |
public static void main (String args[]) throws Exception { | |
ServerSocket ss = new ServerSocket (1300); | |
System.out.println ("DayTime Server Stated: Listening on port 1300."); | |
// 1)As defined in RFC 867 | |
// 2)Link to RFC https://www.rfc-editor.org/rfc/rfc867 | |
// 3)For concurrency, we are using threading! | |
while(true){ | |
Socket soc = ss.accept(); | |
System.out.println ("Got connection: "+soc.toString()); | |
Runnable runner = new Runnable (){ | |
@Override | |
public void run() { | |
try { | |
OutputStreamWriter w = new OutputStreamWriter (soc.getOutputStream()); | |
w.write ((new Date()).toString()); | |
w.flush(); | |
w.close(); | |
soc.close(); | |
} | |
catch (Exception e){ | |
System.out.println (e); | |
} | |
} | |
}; | |
(new Thread(runner)).start(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.net.*; | |
import java.io.*; | |
public class lab3Client{ | |
public static void main (String args[]) throws Exception { | |
Socket soc = new Socket("localhost" , 3141); | |
System.out.println ("Got connection: "+soc.toString()); | |
InputStreamReader r = new InputStreamReader (soc.getInputStream()); | |
BufferedReader br = new BufferedReader (r); | |
System.out.println ("Server: "+br.readLine()); | |
br.close(); | |
r.close(); | |
soc.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.net.*; | |
import java.io.*; | |
public class lab3Server{ | |
public static void main (String args[]) throws Exception { | |
ServerSocket ss = new ServerSocket (3141); | |
System.out.println ("Waiting for connection:"); | |
Socket soc = ss.accept(); | |
System.out.println ("Got connection: "+soc.toString()); | |
OutputStreamWriter w = new OutputStreamWriter (soc.getOutputStream()); | |
w.write ("Hello, World!"); | |
w.flush(); | |
w.close(); | |
soc.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.net.*; | |
import java.io.*; | |
public class lab4_UDP_Echo{ | |
public static void main (String args[]) throws Exception { | |
int portTo = Integer.parseInt(args[1]); | |
int portFrom = Integer.parseInt(args[0]); | |
DatagramSocket s = new DatagramSocket (portFrom); | |
String message = "Hello, UDP Server!"; | |
byte[] messageData = message.getBytes(); | |
s.send(new DatagramPacket(messageData,messageData.length, InetAddress.getLocalHost(),portTo)); | |
for (int i=0;i<5;i++){ | |
s.receive(new DatagramPacket(messageData,messageData.length)); | |
System.out.println("Echoing "+portTo+" :- "+new String (messageData)); | |
s.send(new DatagramPacket(messageData,messageData.length, InetAddress.getLocalHost(),portTo)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.net.*; | |
import java.io.*; | |
// usage: java lab5HalfDuplex <portNumber> | |
// for eg: java lab5HalfDuplex 3141 | |
public class lab5HalfDuplex{ | |
public static void main (String args[]) throws Exception { | |
System.out.println ("Is this Server or Client? [S/c]"); | |
Scanner scn = new Scanner (System.in); | |
String input = scn.nextLine(); | |
Socket s; | |
int port = Integer.parseInt(args[0]); | |
char ch = input.charAt(0); | |
if (ch =='C' || ch == 'c'){ | |
s = new Socket("localhost",port); | |
} | |
else { | |
ServerSocket ss = new ServerSocket(port); | |
s = ss.accept(); | |
} | |
System.out.print ("Enter User Name: "); | |
String name = scn.nextLine(); | |
BufferedReader br = new BufferedReader (new InputStreamReader(s.getInputStream())); | |
OutputStreamWriter w = new OutputStreamWriter (s.getOutputStream()); | |
while (true){ | |
System.out.print("Enter a message: "); | |
String message = scn.nextLine(); | |
if (message.equalsIgnoreCase("close")) break; | |
w.write(name+" : "+message+"\n"); | |
w.flush(); | |
System.out.println (br.readLine()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.net.*; | |
import java.io.*; | |
public class lab6FullDuplex{ | |
public static void main (String args[]) throws Exception { | |
System.out.println ("Is this Server or Client? [S/c]"); | |
Scanner scn = new Scanner (System.in); | |
String input = scn.nextLine(); | |
Socket s; | |
int port = Integer.parseInt(args[0]); | |
char ch = input.charAt(0); | |
if (ch =='C' || ch == 'c'){ | |
s = new Socket("localhost",port); | |
} | |
else { | |
ServerSocket ss = new ServerSocket(port); | |
s = ss.accept(); | |
} | |
new Thread(new lab6FullDuplex.reader(s)).start(); | |
new Thread(new lab6FullDuplex.writer(s)).start(); | |
} | |
public static class reader implements Runnable { | |
public Socket s; | |
public reader(Socket s){ | |
this.s = s; | |
} | |
@Override | |
public void run () { | |
try { | |
InputStreamReader r = new InputStreamReader (s.getInputStream()); | |
while (true) { | |
char dataLine [] = new char [8*1024]; | |
if (r.read(dataLine)!=-1) { | |
System.out.println("Receiver: "+new String(dataLine)); | |
} | |
} | |
} | |
catch (Exception e) { | |
System.out.print(e); | |
} | |
} | |
} | |
public static class writer implements Runnable { | |
public Socket s; | |
public writer(Socket s){ | |
this.s = s; | |
} | |
@Override | |
public void run () { | |
try { | |
OutputStreamWriter w = new OutputStreamWriter (s.getOutputStream()); | |
Scanner scn = new Scanner (System.in); | |
while (true) { | |
//System.out.print("Sender: "); | |
String message = scn.nextLine(); | |
w.write(message); | |
w.flush(); | |
} | |
} | |
catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment