A simple client and server interaction (client sends data to the server, the server would then return the data to the client)
Server
* 1, create a server designated port the ServerSocket (int Port)
* 2, upon receiving a client connection blocking
* 3 send data
* receive a plurality of clients
Mulityserver.java
Package cn.Tcp; / * * server * 1, the server creates a port designated the ServerSocket (int Port) * 2, upon receiving a client connection blocking * 3 send data * receiving a plurality of clients * / Import java.io.DataOutputStream ; Import java.io.IOException; Import the java.net.ServerSocket; Import the java.net.Socket; public class Mulityserver { public static void main (String [] args) throws IOException { // . 1, the server creates the specified port ServerSocket (int Port) the ServerSocket Server = new new the ServerSocket (8488 ); //2, upon receiving a client connection blocking the while ( to true ) { // an infinite loop accept () a client the Socket Socket = server.accept (); System.out.println ( "client establish a connection" ); // 3, the transmission data String msg = "Welcome" ; // output stream the DataOutputStream DOS = new new the DataOutputStream (Socket.getOutputStream ()); dos.writeUTF (MSG); dos.flush (); } } }
Client
Client.java
Package cn.Tcp; Import java.io.DataInputStream; Import java.io.IOException; Import the java.net.Socket; Import a java.net.UnknownHostException; public class Client { public static void main (String [] args) throws UnknownHostException, {IOException // . 1, create client must specify the server and port connections in this case the Socket client = new new the Socket ( "localhost", 8488 ); // 2, reception data DataInputStream DIS = new new DataInputStream (client.getInputStream ()) ; String echo=dis.readUTF(); System.out.println(echo); } }
effect:
Second, to achieve multi-threaded client and server-side interaction (constantly sending client, server constantly returns)
1) method closes the stream
CloseUtil.java
package chatdemo02; import java.io.Closeable; import java.io.IOException; /* * 关闭流的方法 */ public class CloseUtil { public static void closeAll(Closeable... io) { for(Closeable temp:io) { try { if(null!=temp) { temp.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace (); } } } }
2) a thread sending data
Send.java
Package chatdemo02; Import java.io.BufferedReader; Import java.io.DataOutputStream; Import java.io.IOException; Import the java.io.InputStreamReader; Import the java.net.Socket; / * * data transmission thread * / public class the Send the implements {the Runnable // console input stream Private the BufferedReader console; // piped output stream Private the DataOutputStream DOS; // control thread Private Boolean isRunning = to true ; public the Send () { console=new BufferedReader(new InputStreamReader(System.in)); } public Send(Socket client) { this(); try { dos=new DataOutputStream(client.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block isRunning=false; CloseUtil.closeAll(dos,console); } } private String getMsgFromConsole() { try { return Console.ReadLine (); } the catch (IOException E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } return "" ; } / * *. 1, receives data from the console * 2, transmission data * / public void Send () { String MSG = getMsgFromConsole (); IF ( null ! && msg.equals MSG = ( ""! )) { the try { dos.writeUTF (MSG); dos.flush (); // mandatory refresh } the catch (IOException e) { // TODO Auto-generated catch block isRunning=false; CloseUtil.closeAll(dos,console); } } } @Override public void run() { // 线程体 while(isRunning) { send(); } } }
3) receiving data thread
Receive.java
package chatdemo02; /* * 接收线程 */ import java.io.DataInputStream; import java.io.IOException; import java.net.Socket; public class Receive implements Runnable { //输入流 private DataInputStream dis; //线程标识 private boolean isRunning=true; public Receive() { } public Receive(Socket client) { try { dis=new DataInputStream(client.getInputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); isRunning=false; CloseUtil.closeAll(dis); } } /* * 接收数据 */ public String receive() { String msg=""; try { msg=dis.readUTF(); } catch (IOException e) { //e.printStackTrace(); isRunning=false; CloseUtil.closeAll(dis); } return msg; } @Override public void run() { //线程体 while(isRunning) { System.out.println(receive()); } } }
4) server
Server.java
Package chatdemo02; Import java.io.DataInputStream; Import java.io.DataOutputStream; Import java.io.IOException; Import the java.net.ServerSocket; Import the java.net.Socket; / * * Create server * write data: output stream * reading the data: the input stream * / public class Server { public static void main (String [] args) throws IOException { the ServerSocket Server = new new the ServerSocket (9999 ); the Socket Client = server.accept (); // write data //输入流 DataInputStream dis=new DataInputStream(client.getInputStream()); String msg=dis.readUTF(); System.out.println(msg); //输出流 DataOutputStream dos=new DataOutputStream(client.getOutputStream()); dos.writeUTF("服务器----->"+msg); dos.flush(); } }
5) Client
Client.java
Package chatdemo02; Import java.io.BufferedReader; Import java.io.DataInputStream; Import java.io.DataOutputStream; Import java.io.IOException; Import the java.io.InputStreamReader; Import the java.net.Socket; Import the java.net. UnknownHostException; / * * Create client: receiving transmission data + data * data written: the output stream * read data: input stream * input stream output stream in the same process should be independent thread * * * * / public class client { public static void main (String [] args) throwsUnknownHostException, IOException { the Socket Client = new new the Socket ( "localhost", 9984 ); // console input stream new new the Thread ( new new the Send (Client)) Start ();. // path new new the Thread ( new new the Receive (Client)) .start (); } }
Run Server.java Client.java effect is as follows: