无法通过套接字连接

时间:2014-04-06 21:58:47

标签: java sockets

我尝试使用套接字连接连接两台计算机。 我通过输入自己的IP地址在同一台计算机上尝试了此代码。 有用。 但它在我家的两台电脑之间没有工作。 类似的代码在我的大学计算机上工作。 这是服务器端代码

    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;


    public class testserver {
        public static void main(String[] args) {
            ServerSocket listener = null;
            try {
                listener = new ServerSocket(7689);
                Socket other = listener.accept();
                System.out.println("connected");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

这是客户端代码

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


    public class testclient {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter ip");
            String ip=sc.next();
            try {
                Socket s = new Socket(ip,7689);
                System.out.println("Connected");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
服务器计算机上的

我键入了ifconfig 得到这条线

eth0      Link encap:Ethernet  HWaddr f0:1f:af:17:26:db  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:51 errors:0 dropped:0 overruns:0 frame:0
          TX packets:51 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:5083 (5.0 KB)  TX bytes:5083 (5.0 KB)

wlan0     Link encap:Ethernet  HWaddr bc:85:56:49:dc:3d  
          inet addr:192.168.0.48  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::be85:56ff:fe49:dc3d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:62 errors:0 dropped:0 overruns:0 frame:0
          TX packets:94 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:13422 (13.4 KB)  TX bytes:15688 (15.6 KB)

我在服务器inet addr:127.0.0.1中使用此ip地址 接下来我启动了服务器代码 那里什么都没发生

在客户端我输入了ifconfig

ifconfig
eth0      Link encap:Ethernet  HWaddr 9c:b6:54:c5:18:db 
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:269 errors:0 dropped:0 overruns:0 frame:0
          TX packets:269 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:25160 (25.1 KB)  TX bytes:25160 (25.1 KB)

wlan0     Link encap:Ethernet  HWaddr 80:56:f2:49:dc:0c 
          inet addr:192.168.2.13  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::8256:f2ff:fe49:dc0c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3842 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3405 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2981248 (2.9 MB)  TX bytes:1352577 (1.3 MB)

然后我在其他计算机上启动了客户端代码 过了一会儿就出现了

java testclient
Enter ip
192.168.0.48
java.net.SocketException: Invalid argument or cannot assign requested address
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at testclient.main(testclient.java:13)

0 个答案:

没有答案