Java Network Programming TCP protocol Java server and client Java socket programming

Source: Internet
Author: User

An example of a Helloword-level Java Socket communication. Communication process:
Start the Server side and enter a dead loop to listen to a port for connection requests. Then run the client side, the clients make a connection request, the service side to listen to the request after the client sent back to accept the message, the connection established, start a thread to process the request, and then continue to the dead loop to listen to other requests. After the client enters the string, press ENTER to send the data to the server. The server replies to the client data after reading the data. This request has been processed and the thread that started has died. If the client receives a return data other than "OK", the connection request is sent again and the data is sent, and the server initiates a thread for the connection to respond ... The client exits until the returned data received by the client is "OK".

One of the key points in Java is the connection between the server and the client, because it is on the same PC, so set the server's address to "localhost", note that I tried to set other names, but none, this reason also asked the old driver to sue. Also note that the service side before the client run, nonsense not to say, on the code:

Client:

Press CTRL + C to copy the code

Package Webprogramingdemo;

Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.Socket;
Import java.net.UnknownHostException;

public class Socketdemo {

/**
* @param args
* @throws IOException
* @throws unknownhostexception
*/
public static void Main (string[] args) throws IOException {

Socket s=new socket ("192.168.2.103", 10002);
OutputStream Out=s.getoutputstream ();
Out.write ("Java". GetBytes ());
InputStream Is=s.getinputstream ();
byte buf[]=new byte[1024];
int Len=is.read (BUF);
System.out.println (New String (Buf,0,len));
S.close ();
}

}
Press CTRL + C to copy the code
Service side:

Press CTRL + C to copy the code

Package Webprogramingdemo;

Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;

public class Serversocketdemo {

/**
* @param args
* @throws IOException
*/
public static void Main (string[] args) throws IOException {

ServerSocket ss = new ServerSocket (10002);
Socket s = ss.accept ();
String IP = s.getinetaddress (). gethostaddress ();
SYSTEM.OUT.PRINTLN (IP + "..... connected ...");
InputStream in = S.getinputstream ();
int len = 0;
byte[] buf = new byte[1024];
Len = In.read (BUF);
System.out.println (New String (buf, 0, Len));
OutputStream Os=s.getoutputstream ();
Os.write ("Received". GetBytes ());
Os.close ();
S.close ();
Ss.close ();
}

}
Press CTRL + C to copy the code

Http://www.cnblogs.com/ysw-go/p/5323357.html

The server modified code:

Package test;

Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import Java.util.Scanner;

public class Serversocketdemo {

/**
* @param args
* @throws IOException
*/
public static void Main (string[] args) throws IOException {

ServerSocket ss = new ServerSocket (10002);
Socket s = ss.accept ();

while (true)
{

String IP = s.getinetaddress (). gethostaddress ();
SYSTEM.OUT.PRINTLN (IP + "..... connected ...");
InputStream in = S.getinputstream ();
int len = 0;
byte[] buf = new byte[1024];
Len = In.read (BUF);
System.out.println (New String (buf, 0, Len));
OutputStream Os=s.getoutputstream ();
Scanner scan=new Scanner (system.in);
String S1=scan.next ();
Os.write (S1.getbytes ());
Os.write ("Received". GetBytes ());

Os.close ();
S.close ();
Ss.close ();
}


}

}

After the client code has been modified:

Package test2;

Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.Socket;
Import java.net.UnknownHostException;
Import Java.util.Scanner;

public class Socketdemo {

/**
* @param args
* @throws IOException
* @throws unknownhostexception
*/
public static void Main (string[] args) throws IOException {


Socket s=new socket ("localhost", 10002);
while (true)
{

OutputStream Out=s.getoutputstream ();
Scanner scan=new Scanner (system.in);
String S1=scan.next ();
Out.write (S1.getbytes ());
Out.write (S1.getbytes ());
InputStream Is=s.getinputstream ();
byte buf[]=new byte[1024];
int Len=is.read (BUF);
System.out.println (New String (Buf,0,len));
S.close ();
}
}

}

The server is turned on, then the client is turned on, and the customer must first send a message with the server, and then the server sends a message back to the client. End of one communication. The output of the client corresponds to the input of the server, and the output of the server corresponds to the client input.

Java Network Programming TCP protocol Java server and client Java socket programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.