Simple socket client and multi-thread server implementation
In the afternoon, you can try to simplify the implementation. Welcome to the discussion.
Client:
Package com. XiongYi. client;
Import java. io .*;
Import java.net .*;
Public class Client ...{
Public static void main (String [] args) throws IOException ...{
Final String Server_IP = "localhost ";
Final int Server_Port = 1986;
Socket socket = new Socket (Server_IP, Server_Port );
Try ...{
System. out. println ("Client Start ...");
System. out. println ("socket =" + socket );
Socket. setKeepAlive (true );
PrintWriter out = new PrintWriter (
New BufferedWriter
(New OutputStreamWriter
(Socket. getOutputStream (), true );
InputStream netIn = socket. getInputStream ();
BufferedReader bufferedReader = new BufferedReader (
New InputStreamReader (System. in ));
System. out. print ("Enter the request :");
String httpString = bufferedReader. readLine ();
System. out. println (httpString );
Int stick = httpString. indexOf ("//");
// Locate "//" in "HTTP: // localhost: 80/Tomcat/test.html"
If (stick! =-1)
HttpString = httpString. substring (stick + 2 );
Stick = httpString. indexOf ("/");
HttpString = httpString. substring (stick );
System. out. println (httpString );
Out. println ("GET" + httpString + "HTTP/1.1 ");
Out. println ("Accept :*/*");
Out. println ("Accept-Language: zh-cn ");
Out. println ("UA-CPU: x86 ");
Out. println ("Accept-Encoding: gzip, deflate ");
Out. println ("User-Agent: R. C .");
Out. println ("Host: localhost:" + Server_Port );
Out. println ("Connection: Keep-Alive ");
Out. flush ();
// Out. close cannot be used here, or the socket will be closed!
System. out. println ("Reading the page ...");
Byte buf [] = new byte [1024];
Int num = 0;
While (num = netIn. read (buf, 0, buf. length ))! =-1 )...{
System. out. print (new String (buf, 0, num, "GBK "));
}
BufferedReader. close ();
NetIn. close ();
Out. close ();
}
Finally ...{
System. out. println ("closing ..");
Socket. close ();
}
}
}
Multi-threaded server:
Package com. XiongYi. server;
Import java. io .*;
Import java.net .*;
Public class Server extends Thread ...{
Int server_Port;
Public Server (int sp )...{
Server_Port = sp;
}
Public void run ()...{
System. out. println ("A server thread is running ...");
Try ...{
ServerSocket serverSocket = new ServerSocket (server_Port );
System. out. println ("ServerSocket =" + serverSocket );
Socket socket = serverSocket. accept ();
System. out. println ("Server start ...");
System. out. println ("Connection accepted, the socket is" + socket );
BufferedReader in = new BufferedReader (new InputStreamReader (socket
. GetInputStream ()));
PrintWriter out = new PrintWriter (new BufferedWriter (
New OutputStreamWriter (socket. getOutputStream (), true );
Out. println ("Hi, I'm a server ...");
System. out. println ("Reading the request ...");
String outS = "The http request :";
While (outS! = Null )...{
System. out. println (outS = in. readLine ());
}
Out. close ();
In. close ();
Socket. close ();
ServerSocket. close ();
System. out. println ("closing ...");
} Catch (IOException e )...{
E. printStackTrace ();
}
}
Public static void main (String [] args )...{
Int I = 0;
While (I <10 )...{
// Enable a maximum of 10 threads
New Server (1986 + I). run ();
}
}
}