Java-0021-简单Socket编程

2016.7.30

服务端

package com.qingke.socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static final int port=3006;
    public static final String ip="localhost";
    public static void main(String[] args){
        try {
            @SuppressWarnings("resource")
            Socket s = new Socket(ip,port);
            System.out.println("成功连接服务器");
            BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            
            @SuppressWarnings("resource")
            Scanner sc = new Scanner(System.in);
            //一人一条,不能连续发
//          clientMsg=sc.nextLine();
//          while(clientMsg!="bye"){
//              output.write("Client say:"+clientMsg+"\r\n");
//              output.flush();
//              if(input.ready()){
//                  serverMsg=input.readLine();
//                  System.out.println(serverMsg);
//              }
//              clientMsg=sc.nextLine();
//          }

            //可以连续发信息
            Thread  fa = new Thread(){//发
                public void run(){
                    String clientMsg;
                    try {
                        while(true){
                            if(sc.hasNextLine()){
                                clientMsg=sc.nextLine();
                                output.write(clientMsg+"\r");
                                output.flush();
                                if(clientMsg.equals("bye"))
                                    break;
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            Thread  shou=new Thread(){//收
                public void run(){
                    String serverMsg;
                    try {
                        while(fa.isAlive()){
                            if(input.ready()){
                                serverMsg=input.readLine();
                                System.out.println("Server say:"+serverMsg);
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
        
        fa.start();
        shou.start();
                
//      sc.close();
//      s.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            
        }
    }
}

客户端

package com.qingke.socket;

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;
import java.util.Scanner;

public class Server {
    public static final int port=3006;
    
    public static void main(String[] args){
        try {
            @SuppressWarnings("resource")
            ServerSocket ss = new ServerSocket(port);
            System.out.println("成功启动服务器,端口:"+port);
            Socket s = ss.accept();
            System.out.println("成功与客户端建立连接");
            BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

            
            @SuppressWarnings("resource")
            Scanner sc = new Scanner(System.in);
            //一人一条,不能连续发
//          clientMsg=input.readLine();
//          System.out.println(clientMsg);
            
//          while(clientMsg!="bye\r\n"){
//                  serverMsg=sc.nextLine();
//                  output.write("Server say:"+serverMsg+"\r\n");
//                  output.flush();
//              if(input.ready()){
//                  clientMsg=input.readLine();
//                  System.out.println(clientMsg);
//              }
//          }
        
            //可以连续发信息
            Thread shou=new Thread(){//收
                    public void run(){
                        String clientMsg;
                        try {
                            while(true){
                                if(input.ready()){
                                    clientMsg=input.readLine();
                                    System.out.println("Client say:"+clientMsg);
                                    if(clientMsg.equals("bye")){
                                        System.out.println(clientMsg.length());
                                        break;
                                    }
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                };
                
            Thread fa = new Thread(){//发
                    public void run(){
                        String serverMsg;
                        try {
                            while(shou.isAlive()){
                                if(sc.hasNextLine()){
                                    serverMsg=sc.nextLine();
                                    if(serverMsg.equals("bye"))
                                        break;
                                    output.write(serverMsg+"\r");
                                    output.flush();
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                };
            
            shou.start();
            fa.start();
            
//          sc.close();
//          s.close();
//          ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行截图


Java-0021-简单Socket编程_第1张图片
run.png

你可能感兴趣的:(Java-0021-简单Socket编程)