Java Implementation Socket Communication __java

Source: Internet
Author: User

Java implements the communication between the socket server and the client, which is illustrated only with sample code


Server-side code


Main program:

Package network;

Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.net.ServerSocket;
Import Java.net.Socket;

public class Service {public
static int i = 1;
public static void Main (string[] args) throws IOException {
//TODO auto-generated method stub
ServerSocket server Socket = new ServerSocket (5202);
Socket socket = NULL;
while (true) {
socket = serversocket.accept ();
SYSTEM.OUT.PRINTLN ("Client" + (i++) + "connected successfully");
Runnable Runnable = new myrunnable (socket);
Thread thread = new Thread (runnable);
Thread.Start ();
}


Multithreading:

Package network;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;


Import Java.net.Socket;
public class Myrunnable implements runnable{private socket socket;
String [] advices={"eat more fruits", "Eat less junk food", "early to bed and early to rise", "more Exercise", "go out for a walk", "go out to play ah"}; Public myrunnable (Socket sockets) {//TODO auto-generated constructor stub this.socket = socket;} @Override public void R Un () {//TODO auto-generated method stub String advice = Getadvice (); try {inputstreamreader inputstreamreader = new INP
Utstreamreader (Socket.getinputstream (), "UTF-8");
BufferedReader bufferedinputstream = new BufferedReader (InputStreamReader);
PrintWriter printwriter = new PrintWriter (Socket.getoutputstream ());
while (true) {String mstring = Bufferedinputstream.readline ();
System.out.println ("This is from client information:" +mstring);
Printwriter.println ("This is information from the server:" +advice);
Printwriter.flush (); catch (IOException e) {//TODO auto-generatedCatch block E.printstacktrace ();

try {socket.close ();} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();}} Private String Getadvice () {//TODO auto-generated method Stub int random = (int) (Math.random () *advices.length);
Dvices[random];
 }
}


Client code

Package Com.myfirstprac;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.io.PrintWriter;
Import Java.net.Socket;




Import java.net.UnknownHostException; public class Cdateinfo {public void Go () throws Unknownhostexception, IOException {socket socket = new socket ("127.0.0.
1 ", 5202);
PrintWriter printwriter = new PrintWriter (Socket.getoutputstream ());
InputStreamReader InputStreamReader = new InputStreamReader (Socket.getinputstream (), "UTF-8");
BufferedReader BufferedReader = new BufferedReader (InputStreamReader);
Printwriter.println ("hh, I am the client");
Printwriter.flush ();
String msg = Bufferedreader.readline ();
SYSTEM.OUT.PRINTLN (msg);
Printwriter.close ();
Socket.close (); public static void Main (string[] args) throws Unknownhostexception, IOException {//TODO auto-generated method stub C
Dateinfo cdateinfo = new Cdateinfo ();
Cdateinfo.go ();


 }
}

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.