java中的套接字编程问题

时间:2011-03-22 10:28:03

标签: java sockets

我不久前从你的网站上获得了这段代码。

import java.io.*;
import java.net.*;

class sevr implements Runnable{
    public void run() {
        ServerSocket sSkt = null;
        Socket skt = null;
        BufferedReader br = null;
        BufferedWriter bw = null;

        try{
            System.out.println("Server: is about to create socket");
            sSkt = new ServerSocket(6666);
            System.out.println("Server: socket created");
        }
        catch(IOException e){
            System.out.println("Server: socket creation failure");
        }
        try{
            System.out.println("Server: is listening");
            skt = sSkt.accept();
            System.out.println("Server: Connection Established");
        }
        catch(IOException e){
            System.out.println("Server: listening failed");
        }
        try{
            System.out.println("Server: creating streams");
            br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
            System.out.println("Server: stream done");
        }
        catch(IOException e){
            System.out.println("Server: stream failed");
        }
        System.out.println("Server: reading the request");
        try{
            String line = null;
            line = br.readLine();
            System.out.println("Server: client said-> "+ line);
        }
        catch(IOException e){
            System.out.println("Server: reading failed");
        }
        System.out.println("Server: reading fished");

        System.out.println("Server: responding");
        try{
            bw.write("Hi! I am server!\n");
            bw.flush();
        }
        catch(IOException e){
            System.out.println("Server: responding failed");
        }
        System.out.println("Server: responding finished");

        System.out.println("Server: is finishing");
        try {
            br.close();
            bw.close();
            skt.close();
            sSkt.close();
        } catch (IOException e) {
            System.out.println("Server: finishing failed");
        }
        System.out.println("Server: done");
    }
}

class clnt implements Runnable{
    public void run() {
        Socket skt = null;
        BufferedReader br = null;
        BufferedWriter bw = null;

        try{
            System.out.println("Client: about to create socket");
            skt = new Socket(InetAddress.getLocalHost(),6666);
            System.out.println("Client: socket created");
        }
        catch(IOException e){
            System.out.println("Client: socket creation failure");
        }

        try{
            System.out.println("Client: creating streams");
            br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
            System.out.println("Client: stream done");
        }
        catch(IOException e){
            System.out.println("Client: stream failed");
        }
        System.out.println("Client: requesting");
        try{
            bw.write("Hi! I am Client!\n");
            bw.flush();
        }
        catch(IOException e){
            System.out.println("Client: requesting failed");
        }
        System.out.println("Client: requesting finished");
        System.out.println("Client: reading the respond");
        try{
            String line = null;
            line =br.readLine();
            System.out.println("Client: server said-> "+ line);
        }
        catch(IOException e){
            System.out.println("Client: reading failed");
        }
        System.out.println("Client: reading fished");



        System.out.println("Client: is finishing");
        try {
            br.close();
            bw.close();
            skt.close();
        } catch (IOException e) {
            System.out.println("Client: finishing failed");
        }
        System.out.println("Client: done");
    }
}


public class Soc {


    public static void main(String[] args) {
        System.out.println("Main started");
        Thread sThread = new Thread(new sevr());
        Thread cThread = new Thread(new clnt());
        sThread.start();
        cThread.start();
        try {
            sThread.join();
            cThread.join();
        } catch (InterruptedException ex) {
            System.out.println("joining failed");
        }
        System.out.println("Main done");

    }

}

我通过路由器连接到网络。共有3台笔记本电脑连接到网络。我在eclipse上运行了这段代码。代码执行成功,没有返回任何错误。但我怎么知道我的笔记本电脑与哪台笔记本电脑建立连接?我该如何确定?

2 个答案:

答案 0 :(得分:2)

您不会连接到任何其他计算机。该程序在同一台机器上运行客户端和服务器。

skt = new Socket(InetAddress.getLocalHost(),6666);

正如您在此处所见,客户端正在连接到端口6666上的本地主机。服务器正在侦听端口6666上的连接。要连接到另一台计算机,您需要将客户端和服务器代码分开并运行它们。不同的机器。然后,您必须更改上面的行以创建套接字到运行服务器的机器的地址。

答案 1 :(得分:1)

       skt = new Socket(InetAddress.getLocalHost(),6666);

您可以关闭所有其他计算机:)您正在从localhost连接到localhost。

修改客户端代码以连接到另一台计算机上运行的服务器代码后,可以使用getRemoteSocketAddress()方法发现远程对等方的SocketAddress