Establish a simple TCP / IP service with Socket
Java, Socket, TCP / IP, Network Programming
Environment: Windows Server 2003 Prerequisites: Make sure the machine has already installed simple TCP / IP services
If you have not installed this service: Control Panel -> Add or Remove Programs -> Add or Remove Win Components -> Network Services -> Simple TCP / IP Services
After installation, you can write a simple service based on TCP / IP:
Import java.io. *; import java.net. *; public class echoclient {public static void main (string [] args) {string msg = "Send and returns a simple statement!"; try {socket socket = new Socket ( "localhost", 7); BufferedReader is = new BufferedReader (new InputStreamReader (socket.getInputStream ())); PrintWriter out = new PrintWriter (socket.getOutputStream (), true); out.print (msg "/ r" ); Out.flush (); string reply = is.readline (); system.out.println ("Send:" msg); System.out.Println ("Receive:" Reply);} catch (IOException E ) {System.err.println (e);}}}