Java for socket communication

Source: Internet
Author: User

One-to-one communication:

Server-side:

public class Serverdemo {
public static void Main (string[] args) {
ServerSocket serversocket = null;
Socket socket = NULL;
BufferedReader reader = null;
try {
Set the port number on the server side
ServerSocket = new ServerSocket (8081);
Wait for the client to send the request
Socket = Serversocket.accept ();
Receive requests from clients
reader = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
Boolean flag = true;
while (flag) {
String read = Reader.readline ();
if ("End". Equals (read)) {
Flag = false;
}
SYSTEM.OUT.PRINTLN ("Server receives message:" +read);
}
} catch (IOException e) {
E.printstacktrace ();
}finally {
try {
Reader.close ();
Socket.close ();
Serversocket.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}


Client:
public class Clientdemo {
public static void Main (string[] args) {
Socket socket = NULL;
When using PrintWriter, you can no longer call the Flush method to refresh when you call the println, Print method
PrintWriter writer = null;
try {
Client socket specifies the address and port number of the server
Socket = new Socket ("127.0.0.1", 8081);
Send a message to the server
writer = new PrintWriter (new BufferedWriter (New OutputStreamWriter (
Socket.getoutputstream ())));
for (int i = 0; i < i++) {
WRITER.PRINTLN ("Send Message" + i);
}
Writer.println ("End");
} catch (Exception e) {
E.printstacktrace ();
} finally {
try {
System.out.println ("close ...");
Writer.close ();
Socket.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

Multi-client communication
Service Side 1:
public class Serverdouble {
public static void Main (string[] args) {
ServerSocket serversocket = null;
Socket socket = NULL;
try {
Set the port number on the server side
ServerSocket = new ServerSocket (8081);
Wait for the client to send the request
System.out.println ("Waiting to connect ...");
while (true) {
Socket = Serversocket.accept ();
System.out.println ("Connected:" +socket.getport ());
New Serverdoubledemo (socket);
}
} catch (IOException e) {
E.printstacktrace ();
}finally {
try {
Socket.close ();
Serversocket.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
Service Side 2:
public class Serverdoubledemo extends Thread {
Socket socket = NULL;
BufferedReader reader = null;
Public Serverdoubledemo (socket socket) {
This.socket = socket;
try {
reader = new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
Start ();
} catch (IOException e) {
E.printstacktrace ();
}
}

@Override
public void Run () {
try {
Boolean flag = true;
while (flag) {
String str = reader.readline ();
if ("End". Equals (str)) {
Flag = false;
}
SYSTEM.OUT.PRINTLN ("Receive message:" +STR);
}
} catch (IOException e) {
E.printstacktrace ();
}finally {
try {
Reader.close ();
Socket.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

Client Ibid:
public class Clientdemo {
public static void Main (string[] args) {
Socket socket = NULL;
When using PrintWriter, you can no longer call the Flush method to refresh when you call the println, Print method
PrintWriter writer = null;
try {
Client socket specifies the address and port number of the server
Socket = new Socket ("127.0.0.1", 8081);
Send a message to the server
writer = new PrintWriter (new BufferedWriter (New OutputStreamWriter (
Socket.getoutputstream ())));
for (int i = 0; i < i++) {
WRITER.PRINTLN ("Send Message" + i);
}
Writer.println ("End");
} catch (Exception e) {
E.printstacktrace ();
} finally {
try {
System.out.println ("close ...");
Writer.close ();
Socket.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}




Java for socket communication

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.