PackageCn.stat.p4.ipdemo;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classServerdemo {/** * @paramargs *@throwsIOException*/ Public Static voidMain (string[] args)throwsIOException {//creating server-to-imageServerSocket ss=NewServerSocket (1000); //gets the client connected to the image .Socket s=ss.accept (); //gets the input stream through the socket object to read the data sent by the client \BufferedReader bufin=NewBufferedReader (NewInputStreamReader (S.getinputstream ())); //4. Get the output stream of the socket and decorate it. PrintWriter out =NewPrintWriter (S.getoutputstream (),true); String Line=NULL; while((Line=bufin.readline ())! =NULL) {System.out.println (line); //Sendout.println (Line.touppercase ()); } s.close (); Ss.close (); }}
PackageCn.stat.p4.ipdemo;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Socket;Importjava.net.UnknownHostException; Public classClentdemo {/** * @paramargs *@throwsIOException *@throwsunknownhostexception*/ Public Static voidMain (string[] args)throwsunknownhostexception, IOException {//Create a connection pair likeSocket socket=NewSocket ("192.168.1.103", 1000); //get keyboard inputBufferedReader bufr=NewBufferedReader (NewInputStreamReader (system.in)); //gets the output stream pair imagePrintWriter pw=NewPrintWriter (Socket.getoutputstream (),true); //Get Return DataBufferedReader bufin=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); String Line=NULL; while((Line=bufr.readline ())! =NULL) { if(Line.equals ("Over")) Break; System.out.println (line); Pw.println (line); String Uptext=Bufin.readline (); System.out.println (Uptext); } socket.close (); }}
Socket connection case for TCP under Java