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有关,还是... ...? 在线等高人指点!!!谢谢啦!
解决方案 »
- 求助:关于数组类型参数的问题
- java 命令行参数运行错误
- myeclipse编译不了,根目录报错,因为子目录没报错,所以找不出问题在哪儿?大家帮帮忙
- Socket问题?大家过来帮忙看看怎么实现,谢谢了!
- 困扰了我一天的GridBagLayout终于解决了!
- jspsmartupload
- 请教一个有关自定义异常与java自带异常的区别的问题
- 执行java -jar first.jar为什么报Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
- 初学JAVA 请教各位高手
- 使用 Java 程序提高处理数据库记录效率
- 问一下,为什么下面代码关于TreeMap,Comparator会有错误
- 请同志们帮俺看看我的正则哪里出现了问题......
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();
}
}
}
可以用这个查看你的端口用没有被占用:netstat -aon|findstr "6666"
3.在拥有192.168.1.9这个ip地址的PC上运行一下你的程序,把IP改成本地
4.在本机用本地IP测试一下
这是你的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端还能连接么,自己想想是不是这个理,想明白了就把代码改了,在看看效果就知道了
{
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();
}
accept()方法是一个阻塞式的方法,如果没有客户端连接,服务器端会处于等待状态,所以不会执行服务器端的dis.close();和s.close();
估计也是端口被占了
LZ cmd里运行
netstat -ano
找6666端口的PID 打开任务管理器找相应PID 看看什么进程占了