java socket

学习一下关于java的socket实现,client为正常客户端连接服务端,client2为模拟服务端连接客户端,参考了本论坛的帖子,谢谢:
server:
package sockettest;
import java.io.BufferedReader;   
import java.io.DataInputStream;   
import java.io.DataOutputStream;   
import java.io.InputStreamReader;   
import java.net.ServerSocket;   
import java.net.Socket;   
  
public class Server {   
  
    /** 端口号 */  
    public static final int PORT = 9999;   
  
    public static int intflag = 0 ;
    public static void main(String[] args) {   
        System.out.println("Server...\n");  
        Server ser = new Server();   
        ser.sock();   
    }   
  
    public void sock() {   
        try {   
            ServerSocket server = new ServerSocket(PORT);   
            while (true) {   
                // 一旦有堵塞, 则表示服务器与客户端获得了连接   
                Socket client = server.accept();   
                // 处理这次连接   
                new PServer(client);   
            }   
        } catch (Exception e) {   
            System.out.println("Server Exception: " + e.getMessage());   
        }   
    }   
  
    private class PServer implements Runnable {   
  
        private Socket socket;   
  
        public PServer(Socket sock) {   
            socket = sock;   
            new Thread(this).start();   
        }   
  
        public void run() {   
            try {   
                // 读取客户端数据   
                DataInputStream input = new DataInputStream(socket.getInputStream());   
                // 向客户端发送数据   
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());   
                   
                System.out.println("client:"+socket.getInetAddress()+"/"+socket.getPort());
                // 读取客户端数据   
                String strclientinput = input.readUTF() ;
                System.out.println("Info from client: " + strclientinput);  
                
                if("IDESTART".equals(strclientinput))
                {
                	if(intflag==0)
                	{
                		new Thread(new Runnable(){
                			public void run() {
                				try {
                					Thread.currentThread().sleep(20000) ;
                                    intflag = 1 ;
                				} catch (InterruptedException e) {
                					e.printStackTrace();
                				}
                			}}).start() ;
                	}
                	while(true)
                	{
                		if(intflag!=0)
                		{
                			intflag = 0 ;
                			out.writeUTF("IDECLOSE"); 
                			System.out.println("send to client: IDECLOSE");
                            input.close();   
                            out.close();  
                            break ;
                		}
                	}
                }else
                {
                	System.out.print("Please input: \t");   
                    // 发送键盘输入的一行   
                    String s = new BufferedReader(new InputStreamReader(System.in)).readLine();   
                    out.writeUTF(s);   
                    input.close();   
                    out.close();   
                }
  
                
            } catch (Exception e) {   
                System.out.println("Server run Exception: " + e.getMessage());   
            }   
        }   
  
    }   
  
}  

client:
package sockettest;
import java.io.BufferedReader;   
import java.io.DataInputStream;   
import java.io.DataOutputStream;   
import java.io.InputStreamReader;   
import java.net.Socket;   
  
public class Client {   
       
    /** 端口号 */  
    public static final int PORT = 9999;   
       
    /** ip 地址 */  
    public static final String IP = "localhost";   
       
    public static void main(String[] args) {   
        System.out.println("Client...");   
        Socket socket = null;   
        try {   
            System.out.println("when the server reply \"good\" , client will shutdown.\n");   
            while (true) {   
                // 创建一个流套接字并将其连接到指定主机上的指定端口号   
                socket = new Socket(IP, PORT);   
                   
                // 读取服务器端数据   
                DataInputStream input = new DataInputStream(socket.getInputStream());   
                // 向服务器端发送数据   
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());   
  
                System.out.print("input: \t");   
                String str = new BufferedReader(new InputStreamReader(System.in)).readLine();   
                out.writeUTF(str);   
                   
                String ret = input.readUTF();    
                System.out.println("response from server: " + ret);   
                // 如接收到 "good" 则断开连接   
                if ("good".equals(ret)) {   
                    System.out.println("client socket close.");   
                    Thread.sleep(500);   
                    break;   
                }   
            }   
        } catch (Exception e) {   
            System.out.println("client exception: " + e.getMessage());   
        } finally {   
            if (socket != null) {   
                try {   
                    socket.close();   
                } catch (Exception e) {   
                    socket = null;   
                    System.out.println("client finally exception: " + e.getMessage());   
                }   
            }   
        }   
    }   
  
}  

client2:
package sockettest;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class Client2 {

	/** 端口号 */
	public static final int PORT = 9999;

	/** ip 地址 */
	public static final String IP = "localhost";

	public static void main(String[] args) {
		System.out.println("Client...");
		Socket socket = null;
		DataInputStream input = null;
		try {

			// 创建一个流套接字并将其连接到指定主机上的指定端口号
			socket = new Socket(IP, PORT);
			// 读取服务器端数据
			input = new DataInputStream(socket.getInputStream());
			// 向服务器端发送数据
			DataOutputStream out = new DataOutputStream(socket
					.getOutputStream());
			out.writeUTF("IDESTART");
			System.out.println("out.writeUTF(IDESTART);");
		} catch (Exception e) {
			e.printStackTrace();
		}
		while (true) {
			try {
				input = new DataInputStream(socket.getInputStream());
				String ret = input.readUTF();
				if ("IDECLOSE".equals(ret)) {
					System.out.println("client close IDE.");
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

你可能感兴趣的:(java,thread,.net,socket,ide)