Demonstrates mutual access of the client and server side of the transport of TCP
Import Java.io.*;import java.net.*;/* Requirements: The client sends data to the server, and after the server receives it, it feeds back the information to the client. *//* Client: 1, set up socket service, specify to connect host and port. 2. Gets the output stream in the socket stream and writes the data to the stream. Sent over the network to the service side. 3. Get the input stream in the socket stream, get the data from the service side and print it. 4. Close Client Resources */Class tcpclient2{ Public Static void Main(string[] args) throws Exception {Socket s =NewSocket ("192.168.10.111",10005); OutputStream out= S.getoutputstream (); out. Write ("Hello, service side!" ". GetBytes ()); InputStreaminch= S.getinputstream ();byte[] buf =New byte[1024x768];intLen =inch. read (BUF); System. out. println (NewString (BUF,0, Len)); S.close (); }}class tcpserver2{ Public Static void Main(string[] args) throws Exception {ServerSocket SS =NewServerSocket (10005); Socket s = ss.accept (); String IP = s.getinetaddress (). gethostaddress (); System. out. println (ip+"... connected"); InputStreaminch= S.getinputstream ();byte[] buf =New byte[1024x768];intLen =inch. read (BUF); System. out. println (NewString (BUF,0, Len)); OutputStream out= S.getoutputstream (); Thread.Sleep (10000); out. Write ("Dude, you got it, too.". GetBytes ()); S.close (); Ss.close (); }}
JAVA--TCP Transmission