简单的serverSocket通信

Posted guilf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的serverSocket通信相关的知识,希望对你有一定的参考价值。

package com.banksteel.boot;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class Test
{
    public static void main(String[] args)
    {
    	// socket
    	ServerSocket serverSocket = null;
    	PrintWriter out = null;
    	Socket clientsSocket = null;

    	Socket helloSocket = null;
    	BufferedReader in = null;
    	try
		{
    		Boolean flag = true;
    		while(flag)
    		{
    			// 1.端口号
	    		serverSocket = new ServerSocket(9999);
	    		// 2.建立连接
    			clientsSocket = serverSocket.accept();
    			// 4.那边3建立连接后,开始输入内容
        		out = new PrintWriter(clientsSocket.getOutputStream(),true);
        		System.out.println("a:请输入内容:");  
//        		byte[] b = new byte[127];  
//        		System.in.read(); 
//        		int n = System.in.read(b);
//    			 //转换为字符串  
//    	        String s = new String(b,0,n);  
        		Scanner sc=new Scanner(System.in);
        		String s=sc.next();
        		// 得到内容
        		out.println(s);
        		clientsSocket.close();
        		serverSocket.close();
        		
        		helloSocket = new Socket("localhost", 9999);
	    		in = new BufferedReader(new InputStreamReader(helloSocket.getInputStream()));
	    		System.out.println("b:"+in.readLine());
	    		in.close();
	    		helloSocket.close();

        		flag = true;
    		}
		} catch (Exception e)
		{
			// TODO: handle exception
		}
    }
}

  

package com.guilf.springBoot_1;



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class Test
{
    public static void main(String[] args)
    {
    	// socket
    	ServerSocket serverSocket = null;
    	PrintWriter out = null;
    	Socket clientsSocket = null;

    	Socket helloSocket = null;
    	BufferedReader in = null;
    	try
		{
    		Boolean flag = true;
    		while(flag)
    		{
    			// 3.得到端口号,建立了连接
    			helloSocket = new Socket("localhost", 9999);
    			// 得到内容
	    		in = new BufferedReader(new InputStreamReader(helloSocket.getInputStream()));
	    		System.out.println("a:"+in.readLine());
	    		in.close();
	    		helloSocket.close();

	    		// 设置端口号发送消息
	    		serverSocket = new ServerSocket(9999);
    			clientsSocket = serverSocket.accept();
        		out = new PrintWriter(clientsSocket.getOutputStream(),true);
        		System.out.println("b:请输入内容:");  
//        		byte[] b = new byte[127];  
//        		System.in.read(); 
//        		int n = System.in.read(b);
//    			 //转换为字符串  
//    	        String s = new String(b,0,n);  
        		Scanner sc=new Scanner(System.in);
        		String s=sc.next();
        		out.println(s);
        		clientsSocket.close();
        		serverSocket.close();
        		flag = true;
    		}
		} catch (Exception e)
		{
			// TODO: handle exception
		}
    }
    
}

  技术分享图片

 

以上是关于简单的serverSocket通信的主要内容,如果未能解决你的问题,请参考以下文章

Socket简单通信

Java 实例演示-ServerSocket 和 Socket 通信实例

一个最简单的Socket通信例子

简单的通信----使用Socket实现TCP协议

java通过ServerSocket与Socket实现通信

java通过ServerSocket与Socket实现通信