刚刚学网络编程,做了个小小练习一直解决不了

大星King 2019-07-03 09:01:05
为啥我写 socket = new Socket("192.163.1.0",9898); 这个端口号客户端方一直链接不上
求大神指导一下 ,看看我的源码还有其他问题么

public class Demo1 {
/*
* @Author Yixingxing
* @Name client
* @Date 20:06 2019/7/3
* @Param []
* @return
* 客户端
**/
@Test
public void client(){
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
try {
socket = new Socket("192.163.1.0",9898);
outputStream = socket.getOutputStream();
fileInputStream = new FileInputStream(new File("刘诗诗.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = fileInputStream.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
outputStream.write("你好我是客户端".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
/*
* @Author Yixingxing
* @Name server
* @Date 20:07 2019/7/3
* @Param []
* @return
* 服务端
**/
@Test
public void server(){
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
serverSocket = new ServerSocket(9898);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
fileOutputStream = new FileOutputStream(new File("刘诗诗copy.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1){
fileOutputStream.write(buffer,0 ,len);
}
System.out.println(inputStream.read());
System.out.println("数据接收成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(accept != null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}


这是报错信息
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at yixingxing01.com.Demo1.client(Demo1.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

...全文
1571 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
海上的程序猿 2019-07-12
  • 打赏
  • 举报
回复
先本地测试,确认不是代码问题;再排查地址IP的问题,能不能连接访问
  • 打赏
  • 举报
回复
2、确定对方防火墙和自己的防火墙能通过,不行就直接关了
  • 打赏
  • 举报
回复
1、确定ip能通,端口也是打开的。
  • 打赏
  • 举报
回复
先用 ping 192.163.1.0 命令看看这个 IP 是否可以到达
再用 telnet 192.163.1.0 9898 命令看看端口是否可以连通

如果以上都可以的话,你的这个 Socket 理论上就是可以连上的
  • 打赏
  • 举报
回复
连接超时一般是没有连接上,所以首先要确保IP地址存在并且可以连接,其次看端口是否开启,最后看防火墙是不是端口拦截了
大星King 2019-07-06
  • 打赏
  • 举报
回复
引用 9 楼 qq_39936465的回复:
ip不能随便打,必须要有这个ip的pc,不然找谁连接呢?
谢谢谢谢,现在知道啦
大星King 2019-07-06
  • 打赏
  • 举报
回复
谢谢啦,,现在明白了,嘿嘿
安达街风格 2019-07-06
  • 打赏
  • 举报
回复
加油,未来的精英
程序员 beige 2019-07-06
  • 打赏
  • 举报
回复
肯定是一个已知的ip,也就是说服务在那个ip那个端口启动着,客户端才有可能连接上。你随便这个ip找得到才怪。如果是本机起服务,本地起客户端连接,那肯定是127.0.0.1啊
qq_39936465 2019-07-05
  • 打赏
  • 举报
回复
ip不能随便打,必须要有这个ip的pc,不然找谁连接呢?
maradona1984 2019-07-05
  • 打赏
  • 举报
回复
引用 7 楼 大星King 的回复:
[quote=引用 6 楼 C0909 的回复:] 那个Ip的电脑是不是开防火墙给拦了
啊哈,我也不晓的,我随便打的Ip[/quote]
大星King 2019-07-04
  • 打赏
  • 举报
回复
引用 6 楼 C0909 的回复:
那个Ip的电脑是不是开防火墙给拦了

啊哈,我也不晓的,我随便打的Ip
大星King 2019-07-04
  • 打赏
  • 举报
回复
引用 2 楼 一帧残像的回复:
记得都用127.0.0.1测试的。
好,谢谢啦,非常感谢
大星King 2019-07-04
  • 打赏
  • 举报
回复
引用 3 楼 竹子_bamboo的回复:
首先你要确定这个ip你能不能连上的,win的cmd的ping ip,看能不能访问,连不上说明没通,连接是需要在同网段内才行,不是随便的都可以的访问的
好,谢谢啦,现在明白了
竹子_bamboo 2019-07-04
  • 打赏
  • 举报
回复
首先你要确定这个ip你能不能连上的,win的cmd的ping ip,看能不能访问,连不上说明没通,连接是需要在同网段内才行,不是随便的都可以的访问的
老许要老婆么 2019-07-04
  • 打赏
  • 举报
回复
那个Ip的电脑是不是开防火墙给拦了
一帧残像 2019-07-04
  • 打赏
  • 举报
回复
记得都用127.0.0.1测试的。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧