码迷,mamicode.com
首页 > 其他好文 > 详细

简单的serverSocket通信

时间:2018-08-01 14:28:39      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:boolean   stat   oid   puts   image   inpu   info   class   字符   

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通信

标签:boolean   stat   oid   puts   image   inpu   info   class   字符   

原文地址:https://www.cnblogs.com/guilf/p/9400502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!