java实现最简单socket通讯 一对一通信

server

package com.as;
import java.io.IOException;  
import java.io.PrintWriter;  
import java.net.ServerSocket;  
import java.net.Socket;  
import java.util.Scanner;  
  
public class ServerDemo {  
    //服务器  
    public static void testServer(){  
        //创建一个服务器  
        System.out.println("等待客户端连接。。。");  
        PrintWriter pwtoclien = null;  
        Scanner keybordscanner = null;  
        Scanner inScanner = null;  
        ServerSocket ss = null;  
        try {  
            ss = new ServerSocket(6666);  
            //创建一个接收连接客户端的对象  
            Socket socket = ss.accept();  
            System.out.println(socket.getInetAddress()+"已成功连接到此台服务器上。");  
            //字符输出流  
            pwtoclien = new PrintWriter(socket.getOutputStream());  
            pwtoclien.println("已成功连接到远程服务器!"+"\t"+"请您先发言。");  
            pwtoclien.flush();  
            keybordscanner = new Scanner(System.in);  
            inScanner = new Scanner(socket.getInputStream());  
            //阻塞等待客户端发送消息过来  
            while(inScanner.hasNextLine()){  
                String indata = inScanner.nextLine();  
                System.out.println("客户端:"+indata);  
                System.out.print("我(服务端):");  
                String keyborddata = keybordscanner.nextLine();  
                System.out.println("我(服务端):"+keyborddata);  
                pwtoclien.println(keyborddata);  
                pwtoclien.flush();  
            }  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }finally {  
            pwtoclien.close();  
            keybordscanner.close();  
            inScanner.close();  
            try {  
                ss.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
    public static void main(String[] args) {  
        testServer();  
    }  
}

  client

package com.as;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class ClientDemo {
	public static void testClient(){
		System.out.println("正在向服务器请求连接。。。");
		Socket socket = null;
		Scanner keybordscanner = null;
		Scanner inScanner = null;
		PrintWriter pwtoserver = null;
		try {
			socket = new Socket("192.168.10.40", 6666);
			inScanner = new Scanner(socket.getInputStream()); 
			System.out.println(inScanner.nextLine());
			pwtoserver = new PrintWriter(socket.getOutputStream());
			System.out.print("我(客户端):");
			//先读取键盘录入方可向服务端发送消息
			keybordscanner = new Scanner(System.in);
			while(keybordscanner.hasNextLine()){
				String keyborddata = keybordscanner.nextLine();
				//展示到己方的控制台
				System.out.println("我(客户端):"+keyborddata);
				//写到服务端的的控制台
				pwtoserver.println(keyborddata);
				pwtoserver.flush();
				//阻塞等待接收服务端的消息
				String indata = inScanner.nextLine();
				System.out.println("服务端:"+indata);
				System.out.print("我(客户端):");
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			keybordscanner.close();
			pwtoserver.close();
			inScanner.close();
			try {
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	public static void main(String[] args) {
		testClient();
	}
}

  

posted @ 2018-05-14 11:42  dafengchui  阅读(2686)  评论(0编辑  收藏  举报