Home >Java >javaTutorial >Java Example - ServerSocket and Socket Communication Example

Java Example - ServerSocket and Socket Communication Example

黄舟
黄舟Original
2017-01-21 10:54:261486browse

The following example demonstrates how to implement the client to send a message to the server, the server receives the message and reads the output, and then writes it out to the client and the client receives the output.

1. Establish server side

  • Server establishes communication ServerSocket

  • Server establishes Socket to receive client connection

  • Establish an IO input stream to read the data sent by the client

  • Establish an IO output stream to send data messages to the client

Server-side code:

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import 
java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.ServerSocket;
import java.net.Socket;public class Server {
   public static void main(String[] args) {
      try {
         ServerSocket ss = new ServerSocket(8888);
         System.out.println("启动服务器....");
         Socket s = ss.accept();
         System.out.println("客户端:"+s.getInetAddress().getLocalHost()+"已连接到服务器");
         
         BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
         //读取客户端发送来的消息
         String mess = br.readLine();
         System.out.println("客户端:"+mess);
         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
         bw.write(mess+"\n");
         bw.flush();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }}

The output result of running the above code is:

启动服务器....

2. Establish the client

  • Create Socket communication and set up communication The IP and Port of the server

  • Establish an IO output stream to send data messages to the server

  • Establish an IO input stream to read the data messages sent by the server

Client code:

/*
 author by w3cschool.cc
 Main.java
 */import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;
 import java.io.InputStreamReader;import java.io.OutputStream;
 import java.io.OutputStreamWriter;import java.net.Socket;
 import java.net.UnknownHostException;public class Client {
   public static void main(String[] args) {
      try {
         Socket s = new Socket("127.0.0.1",8888);
         
         //构建IO
         InputStream is = s.getInputStream();
         OutputStream os = s.getOutputStream();
         
         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
         //向服务器端发送一条消息
         bw.write("测试客户端和服务器通信,服务器接收到消息返回到客户端\n");
         bw.flush();
         
         //读取服务器返回的消息
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         String mess = br.readLine();
         System.out.println("服务器:"+mess);
      } catch (UnknownHostException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }}

The output result of running the above code is:

服务器:测试客户端和服务器通信,服务器接收到消息返回到客户端

The above is the content of the Java instance-ServerSocket and Socket communication instance, more For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn