Import java.net. *;
Import java.io. *;
Public class ClientSocketDemo
{
//Declare the client socket object socket
Socket socket = null
//Declare the client data iostream.
DataInputStream in
DataOutputStream out
//Declares a string array object response to store information received from the server.
String response [];
//When executing, the local server is local to the constructor with no parameters, and the default port is 10745.
public ClientSocketDemo()
{
attempt
{
//Create a client socket with the local server address and port number 10745.
socket = new Socket("localhost ", 10745);
//Create client data iostream for sending or receiving data to the server.
in = new data inputstream(socket . getinputstream());
out = new data output stream(socket . get output stream());
//Get the client address and port number.
string IP = string . value of(socket . get local address());
string port = string . value of(socket . getlocalport());
//Send data to the server
out.writeUTF("Hello Server。 This connection is from the client. );
out . write utf(IP);
Out.writeUTF (port);
//Receive data from the server
Response = new string [3];
for(int I = 0; I < Response. Length; i++)
{
response[I]= in . read utf();
system . out . println(response[I]);
}
}
catch(UnknownHostException e){ e . printstacktrace(); }
catch(io exception e){ e . printstacktrace(); }
}
//Construction method when there are parameters during execution. This parameter specifies the server address, and the default port is 10745.
Public ClientSocketDemo (string host name)
{
attempt
{
//Create a client socket. The hostname parameter specifies the server address, and the port number is 10745.
Socket = new socket (host name,10745);
in = new data inputstream(socket . getinputstream());
out = new data output stream(socket . get output stream());
string IP = string . value of(socket . get local address());
string port = string . value of(socket . getlocalport());
out.writeUTF("Hello Server。 This connection is from the client. );
out . write utf(IP);
Out.writeUTF (port);
Response = new string [3];
for(int I = 0; I < Response. Length; i++)
{
response[I]= in . read utf();
system . out . println(response[I]);
}
}
catch(UnknownHostException e){ e . printstacktrace(); }
catch(io exception e){ e . printstacktrace(); }
}
//Construction method when there are two parameters during execution. The first parameter hostname specifies the server address.
//The first parameter serverPort specifies the server port number.
Public ClientSocketDemo (string host name, string server port)
{
attempt
{
Socket = new socket (hostname, integer.parseint (serverport));
in = new data inputstream(socket . getinputstream());
out = new data output stream(socket . get output stream());
string IP = string . value of(socket . get local address());
string port = string . value of(socket . getlocalport());
out.writeUTF("Hello Server。 This connection is from the client. );
out . write utf(IP);
Out.writeUTF (port);
Response = new string [3];
for(int I = 0; I < Response. Length; i++)
{
response[I]= in . read utf();
system . out . println(response[I]);
}
}
catch(UnknownHostException e){ e . printstacktrace(); }
catch(io exception e){ e . printstacktrace(); }
}
Public static void main(String[] args)
{
string comd[]= args;
If (command length == 0)
{
System.out.println ("Use local host (127.0.0. 1) and default port");
clientsocket demo demo = new clientsocket demo();
}
else if(comd.length == 1)
{
System.out.println ("use default port");
ClientSocketDemo demo = new ClientSocketDemo(args[0]);
}
else if(comd.length == 2)
{
System.out.println ("host name and port are named by users");
ClientSocketDemo demo = new ClientSocketDemo(args[0],args[ 1]);
}
else system . out . println(" ERROR ");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ServerSocketDemo.java server-side Java source code
Import java.net. *;
Import java.io. *;
Public class ServerSocketDemo
{
//declare the ServerSocket class object.
ServerSocket server socket;
//Declare and initialize the server-side listening port number constant.
Public static final int port =10745;
//Declare server-side data iostream.
DataInputStream in
DataOutputStream out
//Declare InetAddress class object ip, which is used to obtain information such as server address and port number.
InetAddress ip = null
//Declares a string array object request to store the information sent by the client.
String request [];
Public server SocketDemo ()
{
Request = new string [3]; //Initialize string array
attempt
{
//Get the address information of the local server
IP = inet address . getlocalhost();
//Take PORT as the service port number, and create a serverSocket object to listen to the connection on this port.
server socket = new server socket(PORT);
//Create an object Socket of the socket class, which is used to save the client socket object connected to the server.
socket socket = server socket . accept();
System.out.println ("This is the server:"+string.valueof (IP)+port);
//Create server-side data iostream, which is used to receive or send data to the client.
in = new data inputstream(socket . getinputstream());
out = new data output stream(socket . get output stream());
//Receive and display the data information sent by the client.
request[0]= in . read utf();
request[ 1]= in . read utf();
request[2]= in . readutf();
System.out.println ("The message received from the client is:");
system . out . println(request[0]);
system . out . println(request[ 1]);
system . out . println(request[2]);
//Send data to the client
Out.writeUTF ("Hello client!" );
Out.writeUTF ("Your ip is:"+request [1]);
Out.writeUTF ("Your port is:"+request [2]);
}
catch(io exception e){ e . printstacktrace(); }
}
Public static void main(String[] args)
{
serversocket demo demo = new serversocket demo();
}
}