import java.io.*;
import java.net.*;public class TCPServer 
{
public static void main(String[] args)
{
try
{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
System.out.println(dis.readUTF());
dis.close();
s.close();
}

catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;public class TCPClient 
{
public static void main(String[] args) 
{
try
{
Socket s=new Socket("192.168.1.9",6666);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeUTF("Hello,Server!");
dos.flush();
dos.close();
s.close();
}

catch(Exception e)
{
e.printStackTrace();
}
}
}
[code=Java]
运行了一番,出现了 java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
问一下是否与模拟的IP有关,还是... ...? 在线等高人指点!!!谢谢啦!

解决方案 »

  1.   

    你把192.168.1.9改成localhost试试。。
      

  2.   

    TCPClient:package first;import java.io.*;
    import java.net.*;public class TCPClient {
    public static void main(String[] args) {
    try {
    Socket s = new Socket("127.0.0.1", 6666);//这里请注意
    OutputStream os = s.getOutputStream();
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeUTF("Hello,Server!");
    dos.flush();
    dos.close();
    s.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    TCPServer package first;import java.io.*;
    import java.net.*;public class TCPServer {
    public static void main(String[] args) {
    try {
    ServerSocket ss = new ServerSocket(6666);
    Socket s = ss.accept();
    DataInputStream dis = new DataInputStream(s.getInputStream());
    System.out.println(dis.readUTF());
    dis.close();
    s.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  3.   

    1.端口占用,改成其他不常用的端口
    可以用这个查看你的端口用没有被占用:netstat -aon|findstr "6666"
      

  4.   

    2.你把拥有192.168.1.9这个ip地址的PC的防火墙等等东西都关掉,试一下
    3.在拥有192.168.1.9这个ip地址的PC上运行一下你的程序,把IP改成本地
    4.在本机用本地IP测试一下
      

  5.   

    估计你是刚刚开始学socket的吧,其实我也是才学不久,不过看了你的代码发现并不是你的socket连接不成功,而是你编程的逻辑出了问题;请注意我给你做出红色标记的部分
    这是你的server端
     try
            {
                ServerSocket ss=new ServerSocket(6666);            
                Socket s=ss.accept();
                DataInputStream dis=new DataInputStream(s.getInputStream());
                System.out.println(dis.readUTF());
                dis.close();
                s.close();
            }
            
            catch(Exception e)
            {
                e.printStackTrace();
            }这是你的client端连接的代码,也请注意我给你做出红色标记的语句try
    {
    Socket s=new Socket("192.168.1.9",6666);
    OutputStream os=s.getOutputStream();
    DataOutputStream dos=new DataOutputStream(os);
    dos.writeUTF("Hello,Server!");
    dos.flush();
    dos.close();
    s.close(); 
    }catch(Exception e)
    {
    e.printStackTrace();
    }你在刚刚建立好serverscoket和输入输出流,你就把它们都关了,服务端这样做的效果就如同你家人客人来敲门,你去开门而在客人还没有进来你就把门给关了,这样客人还能进来吗,你这样client端还能连接么,自己想想是不是这个理,想明白了就把代码改了,在看看效果就知道了
      

  6.   

     try
            {
                ServerSocket ss=new ServerSocket(6666);            
                Socket s=ss.accept();
                DataInputStream dis=new DataInputStream(s.getInputStream());
                System.out.println(dis.readUTF());
                dis.close();
                s.close();
            }
            
            catch(Exception e)
            {
                e.printStackTrace();
            }try
    {
    Socket s=new Socket("192.168.1.9",6666);
    OutputStream os=s.getOutputStream();
    DataOutputStream dos=new DataOutputStream(os);
    dos.writeUTF("Hello,Server!");
    dos.flush();
    dos.close();
    s.close(); 
    }catch(Exception e)
    {
    e.printStackTrace();
    }
      

  7.   

    你的代码在我机器的上运行完全没问题,建议你把Socket s = new Socket("192.168.1.9",6666);中的IP改为127.0.0.1(代表本机),如果还不行,你再把端口6666修改一下(可能6666端口被其它程序占用)。再补充一下楼上的,其实这和close()方法没关系,服务器端有Socket s = ss.accept();
    accept()方法是一个阻塞式的方法,如果没有客户端连接,服务器端会处于等待状态,所以不会执行服务器端的dis.close();和s.close();
      

  8.   

    IP  可以是127.0.0.1; localhost;也可以是本地连接地址,LZ的内网地址就可以
    估计也是端口被占了   
    LZ cmd里运行
    netstat -ano 
    找6666端口的PID  打开任务管理器找相应PID 看看什么进程占了