1
1 package hello; 2 3 Import Java. io. datainputstream; 4 Import java.net. *; 5 6 public class helloworld 7 {8 public static void main (string [] ARGs) throws exception 9 {10 serversocket S = new serversocket (6666); // server port number, used to listen to 11 socket Ss = S. accept (); // block type, do not receive do not continue 12 system. out. println ("A is accepted;"); 13 datainputstream Dis = new datainputstream (ss. getinputstream (); // 14 system. out. println (DIS. readutf (); // 15 dis. close (); 16 s. close (); 17} 18} 19 20 package ha; 21 import Java. io. *; 22 Import java.net. *; 23 Import Java. util. *; 24 public class wori25 {26 public static void main (string ARGs []) throws exception27 {28 socket S = new socket ("127.0.0.1", 6666 ); // connect to the local IP address and connect to port 29 dataoutputstream dos = new dataoutputstream (S. getoutputstream (); // connect to the above S30 dos. writeutf ("wori"); 31 dos. close (); 32 s. close (); 33} 34}
2
1 package hello; 2 import java.io.*; 3 import java.net.*; 4 public class helloworld 5 { 6 public static void main(String[] args)throws Exception 7 { 8 ServerSocket s = new ServerSocket(6666); 9 Socket ss = s.accept(); 10 InputStreamReader is = new InputStreamReader(System.in);11 BufferedReader br = new BufferedReader(is);12 OutputStreamWriter os = new OutputStreamWriter(ss.getOutputStream());13 BufferedWriter bw = new BufferedWriter(os);14 InputStreamReader is2 = new InputStreamReader(ss.getInputStream());15 BufferedReader br2 = new BufferedReader(is2);16 String st = null;17 System.out.println(br2.readLine());18 while((st = br.readLine()) != null)19 {20 if(st.equalsIgnoreCase("exit"))21 {22 break;23 }24 System.out.println(br2.readLine());25 bw.write(st);26 }27 bw.close();28 br2.close();29 br.close();30 s.close();31 }32 }33 34 package ha;35 import java.io.*;36 import java.net.*;37 import java.util.*;38 public class wori39 {40 public static void main(String args[])throws Exception41 {42 Socket s = new Socket("127.0.0.1", 6666);43 InputStreamReader is = new InputStreamReader(System.in);44 BufferedReader br = new BufferedReader(is);45 OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());46 BufferedWriter bw = new BufferedWriter(os);47 InputStreamReader is2 = new InputStreamReader(s.getInputStream());48 BufferedReader br2 = new BufferedReader(is2);49 System.out.println("Connceted;");50 String st;51 while((st = br.readLine()) != null)52 {53 if(st.equalsIgnoreCase("exit"))54 {55 break;56 }57 bw.write(st);58 System.out.println(br2.readLine());59 }60 System.out.println("The programme is closed;");61 br.close();62 br2.close();63 bw.close();64 s.close();65 }66 }
3
UDP
1 package ha; 2 Import Java. io. *; 3 Import java.net. *; 4 Import Java. util. *; 5 public class wori 6 {7 public static void main (string ARGs []) throws exception 8 {9 byte A [] = new byte [1024]; // used to store received data 10 bytes rampacket dp = new bytes rampacket (A, 1024); // 11 bytes ramsocket DS = new bytes ramsocket (5678); // port number 12 while (true) 13 {14 DS. receive (DP); // receives 15 system. out. println (new string (A, 0, DP. getlength (); // convert byte array a from 0 to the end to string16} 17} 18} 19 20 21 22 package hello; 23 Import Java. io. *; 24 import java.net. *; 25 public class helloworld26 {27 public static void main (string [] ARGs) throws exception28 {29 byte A [] = (new string ("wocao ")). getbytes (); // convert sting to a byte array of 30 bytes rampacket dp = new bytes rampacket (A,. length, new inetsocketaddress ("127.0.0.1", 5678); // 31 datagramsocket DS = new datagramsocket (9999); // set the client port number to 32 Ds. send (DP); // 33 Ds. close (); 34} 35}