Java socket building blocked TCP traffic

Source: Internet
Author: User

1. Server-side

1. Create a ServerSocket object to specify the listening port in the constructor;

private int port = 8000;
private ServerSocket serverSocket;
……
serverSocket = new ServerSocket(port);

2. Server-side invoke the Accept () method of the ServerSocket object, which listens on the port, waits for a client's connection request, and if a connection request is received, the Accept () method returns a Socket object. This socket object will form a communication line with the client's socket object;

Socket socket = null;
socket = serverSocket.accept(); // 等待客户连接

The 3.Socket class provides the getInputStream () method and the Getoutputstream () method.

InputStream socketin = Socket.getinputstream ();

OutputStream socketout = Socket.getoutputstream ();

Source Code Echoserver.java

public class Echoserver {
private int port = 8000;
Private ServerSocket ServerSocket;    
Public Echoserver () throws IOException {
ServerSocket = new ServerSocket (port);
System.out.println ("Server Start");
}
public string Echo (String msg) {
return "echo:" + msg;
}
Private PrintWriter getwriter (socket socket) throws IOException {
OutputStream socketout = SOCKET.GETOUTPU Tstream ();
return new PrintWriter (Socketout, true);
}
Private BufferedReader getreader (socket socket) throws IOException {
InputStream socketin = Socke T.getinputstream ();
return new BufferedReader (new InputStreamReader (Socketin));      
}
public void service () {
while (true) {
Socket socket = NULL;
           try {
socket = serversocket.accept ()//Waiting for client to connect
System.out.println ("New Connection accepted"
+ socket.getinetaddress () + ":" + SoCket.getport ());
BufferedReader br = getreader (socket);
PrintWriter pw = getwriter (socket);
String msg = null;          
while (msg = Br.readline ())!= null) {
System.out.println (msg);
Pw.println (Echo (msg));        
if (msg.equals ("Bye"))//If the customer sends a message of "Bye", ends the communication
break;
     
} catch (IOException e) {
E.printstacktrace ();
Finally {
try {
if (socket!= null)
Socket.close ();//Disconnect
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
public static void Main (String args[]) throws IOException {
N EW Echoserver () service ();
}
}

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.