java Socket

1 通过ping查看网络是否畅通,ping工作在TCP层,不能证明主机上是否开发某个端口。

image

查看机器中正在与哪些网络服务通信,使用netstat –an,由于只有TCP是面向协议的,因此只有TCP协议有状态说明。

image

image

2 创建简单服务器:

public class ChatServer {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ChatServer cs = new ChatServer();
        cs.setUpServer(9090);
    }

    private void setUpServer(int port) {
        // TODO Auto-generated method stub
        try {
            ServerSocket server = new ServerSocket(port);
            System.out.println("服务器创建成功!" + port);
            while(true){//让服务器循环等待
                Socket client = server.accept();
                OutputStream out = client.getOutputStream();
                InputStream ins = client.getInputStream();
                String s = "你好,欢迎连接到服务器\r\n";
                byte[] data = s.getBytes();
                out.write(data);
                out.flush();
               
                int in=0;//一个字节一个字节地读取客户端输入
                while(in!=13){//输入不是回车
                    in=ins.read();
                    System.out.println("读到的是:"+in);
                }
                System.out.println("客户按了回车,退出"+in);
                client.close();
            }
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

启动Telnet连接服务器:

image

imageimage

3 接收客户端字符串,并返回响应

private void setUpServer(int port) {
    // TODO Auto-generated method stub
    try {
        ServerSocket server = new ServerSocket(port);
        System.out.println("服务器创建成功,等待连接......!" + port);
        while (true) {// 让服务器循环等待
            Socket client = server.accept();
            System.out.println("Incoming Client"
                    + client.getRemoteSocketAddress());
           processChat(client);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void processChat(Socket client) throws IOException {
    // TODO Auto-generated method stub
    OutputStream out = client.getOutputStream();
    InputStream ins = client.getInputStream();
    String s = "你好,欢迎来到服务器!";
    byte[] data = s.getBytes();
    out.write(data);
    out.flush();

    String inputS = readString(ins);

    while (!inputS .endsWith("bye")) {
        System.out.println("客户端说:" + inputS);
        s = "服务器收到:" + inputS + "\r\n";
        data = s.getBytes();
        out.write(data);
        out.flush();
        inputS = readString(ins);
    }
    client.close();
}

private String readString(InputStream ins) throws IOException {
    // TODO Auto-generated method stub
    StringBuffer sb = new StringBuffer();
    char c = 0;
    while (c != 13) {
        int i = ins.read();
        c = (char) i;
        sb.append(c);
    }
    String inputS = sb.toString().trim();
    return inputS;
}

imageimage

4 多线程服务器端

private void setUpServer(int port) {
        // TODO Auto-generated method stub
        try {
            ServerSocket server = new ServerSocket(port);
            System.out.println("服务器创建成功,等待连接......!" + port);
            while (true) {// 让服务器循环等待
                Socket client=server.accept();
                ServerThread st=new ServerThread(client);
                st.start();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

ServerThread.java

public class ServerThread extends Thread{
    Socket client;
   public ServerThread(Socket sc){
        this.client=sc;
    }
    public void run(){
        try {
            processChat(this.client);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private void processChat(Socket client) throws IOException {
        // TODO Auto-generated method stub
        OutputStream out = client.getOutputStream();
        InputStream ins = client.getInputStream();

……

imageimageimage

posted @ 2011-04-13 14:12  朱旭东  阅读(1137)  评论(0编辑  收藏  举报