A simple c/s multi-threaded application that can be used with a slight Extension
/* Socket is the core of network applications. socket is an indispensable element in network applications on servers or clients.
* Common applications on servers include FTP server, Mail Server (SMTP, POP3, IMAP4 protocol), and Web (HTTP protocol)
* To create a server socket application, follow these steps:
* 1. Establish a socket on the server and listen for client connection requests.
* 2. When the server detects a connection request from the client, it receives the request and establishes the socket of the client.
* The basis for client connection and subsequent processing of sent and received data. At this point, the socket communication link between the server and the client is completed.
* 3. Processing information from the client, generally called a request, can be considered as the client's command requirements. For example, HTTP Communication Protocol URL requests,
* Or FTP commands for FTP communication protocols (such as get and put)
* 4. Based on the requests sent from the client, the server must be processed by the program logic, and send back the corresponding reception result or error message
* If the HTTP server sends the HTML webpage content back to the client, the FTP server sends the result of the FTP command back.
* 5. After the program completes data or command processing, it closes the socket communication link.
*/
/*
Simpleserver. Java
*/
Import java.net .*;
Import java. Io .*;
Public class simpleserver
{
Private Static serversocket;
Private Static listenclient listen;
Public static void main (string [] ARGs) throws exception
{
Int port;
If (ARGs. Length = 0)
{
System. Out. println ("Usage: Java simpleserver [port]");
System. Exit (1 );
}
Port = integer. parseint (ARGs [0]);
Startserver (port );
}
Public static void startserver (INT port) throws exception
{
Try
{
Serversocket = new serversocket (port );
Thread thread = new thread (New listenclient (serversocket ));
Thread. Start ();
}
Catch (ioexception ex)
{
Ex. printstacktrace ();
}
}
}
Class listenclient implements runnable
{
Private serversocket;
Private socket clientsocket;
Datainputstream in;
Dataoutputstream out;
Public listenclient (serversocket) throws exception
{
This. serversocket = serversocket;
}
Public void run ()
{
Try
{
While (true)
{
Clientsocket = serversocket. Accept ();
System. Out. println ("connection from" + clientsocket. getinetaddress (). gethostaddress ());
In = new datainputstream (clientsocket. getinputstream ());
Out = new dataoutputstream (clientsocket. getoutputstream ());
String linesep = system. getproperty ("line. separator"); // line separator, that is, line breaks
Inetaddress ADDR = serversocket. getinetaddress (). getlocalhost ();
String outdata = "server information:" + linesep + "local host:" + serversocket. getinetaddress (). getlocalhost () + linesep + "port:" + serversocket. getlocalport ();
Byte [] outbyte = outdata. getbytes ();
Out. Write (outbyte, 0, outbyte. Length );
}
}
Catch (exception ex)
{
Ex. printstacktrace ();
}
}
};
/* The process of the client socket application is similar to that of the server socket application. The biggest difference is:
* 1. The server socket application is mainly used to listen for and receive client connections, while the client's socket
* The application is used to try to establish a connection with the server.
* 2. The client socket application sends information commands to the server and receives the results returned by the server;
* The server-side socekt application processes the command logic and sends the result or error information to the client.
*
* Common Client Applications include chat client, FTP client, POP3 client, SMTP client, and Telnet client.
* The procedure for creating a client socket application is as follows:
* 1. When creating a client socket, you must specify the Host Name (or IP) and Internet
* Communication Port
* 2. Send specific information or instructions to the server
* 3. Receive the execution results or error messages returned by the server, and display them in a specific format. For example
* HTML display
* 4. When the client does not need to process the server, it closes the socekt communication link.
*/
/*
Simpleclient. Java
*/
Import java.net .*;
Import java. Io .*;
Public class simpleclient
{
Private Static Socket socket;
Public static void main (string [] ARGs) throws exception
{
String host;
Int port;
If (ARGs. Length <2)
{
System. Out. println ("Usage: Java simpleclient [remote IP/Host] [port]");
System. Exit (1 );
}
Host = ARGs [0];
Port = integer. parseint (ARGs [1]);
Connectserver (host, Port );
}
Public static void connectserver (string host, int port)
{
Try
{
Socket = new socket (inetaddress. getbyname (host), Port );
Datainputstream in = new datainputstream (socket. getinputstream ());
Dataoutputstream out = new dataoutputstream (socket. getoutputstream ());
Byte [] inbyte = new byte [1, 1024];
In. Read (inbyte );
String response = new string (inbyte, 0, inbyte. Length );
System. Out. println ("Message from server :");
System. Out. println (response. Trim ());
}
Catch (unknownhostexception E)
{
E. printstacktrace ();
}
Catch (ioexception ex)
{
Ex. printstacktrace ();
}
Finally
{
Try
{
Socket. Close ();
}
Catch (ioexception E)
{
E. printstacktrace ();
}
}
}
}