黑马程序员技术交流社区
标题:
基于TCP协议的C/S通信,两者不能正常通信
[打印本页]
作者:
GUO18735177413
时间:
2015-1-13 10:30
标题:
基于TCP协议的C/S通信,两者不能正常通信
我刚写了一个基于TCP的聊天小程序,客户端运行时有如下报错:
Exception in thread "main" java.net.ConnectException: Connection timed out: conn
ect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketI
mpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.ja
va:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocket
Impl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java
:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
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 ClientB.main(TCPChat.java:5)
源代码如下:
import java.io.*;
import java.net.*;
class ClientB {
public static void main(String[] args) throws Exception {
Socket s = new Socket( "202.199.2.134" , 8080);
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in ));
BufferedWriter bufout= new BufferedWriter( new OutputStreamWriter(s.getOutputStream()));
BufferedReader bufIn = new BufferedReader( new InputStreamReader(s.getInputStream()));
String line = null ;
while ((line = buf.readLine()) != null ) {
if ( "over" .equals(line))
break ;
bufout.write(line);
bufout.newLine();
bufout.flush();
String str=bufIn.readLine();
System.out.print( "server:" +str);
}
s.close();
}
}
class ServerA {
public static void main(String []agrs) throws Exception {
ServerSocket ss= new ServerSocket(8080);
Socket s= ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+ "我们连接成功了!" );
BufferedReader buf = new BufferedReader( new InputStreamReader(s.getInputStream()));
BufferedWriter bufout= new BufferedWriter( new OutputStreamWriter(s.getOutputStream()));
String line= null ;
while ((line=buf.readLine())!= null ){
System.out.println(line);
StringBuffer re= new StringBuffer(line);
String str=re.reverse().toString();
System.out.println(re+ "++ ++" +str);
bufout.write(str);
bufout.newLine();
bufout.flush();
}
s.close();
}
import java.io.*;
import java.net.*;
class ClientB {
public static void main(String[] args) throws Exception {
Socket s = new Socket( "202.199.2.134" , 8080);
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in ));
BufferedWriter bufout= new BufferedWriter( new OutputStreamWriter(s.getOutputStream()));
BufferedReader bufIn = new BufferedReader( new InputStreamReader(s.getInputStream()));
String line = null ;
while ((line = buf.readLine()) != null ) {
if ( "over" .equals(line))
break ;
bufout.write(line);
bufout.newLine();
bufout.flush();
String str=bufIn.readLine();
System.out.print( "server:" +str);
}
s.close();
}
}
class ServerA {
public static void main(String []agrs) throws Exception {
ServerSocket ss= new ServerSocket(8080);
Socket s= ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+ "我们连接成功了!" );
BufferedReader buf = new BufferedReader( new InputStreamReader(s.getInputStream()));
BufferedWriter bufout= new BufferedWriter( new OutputStreamWriter(s.getOutputStream()));
String line= null ;
while ((line=buf.readLine())!= null ){
System.out.println(line);
StringBuffer re= new StringBuffer(line);
String str=re.reverse().toString();
System.out.println(re+ "++ ++" +str);
bufout.write(str);
bufout.newLine();
bufout.flush();
}
s.close();
}
}
复制代码
}
客户端和服务器端不能进行正常通信,是不是输入流的问题?求指点。
作者:
Piaoyu
时间:
2015-1-13 11:56
聊天程序是面向非连接的,要使用UDP。TCP是面向连接的,你不可能和服务器聊天啊。
作者:
Piaoyu
时间:
2015-1-13 11:58
服务器,是不能主动向客户端发送信息的。
作者:
kerner
时间:
2015-1-13 13:01
代码没有问题啊,8080端口是www服务器在使用的端口,你改成其他端口,别低于1000(系统软件使用)就好。
还有IP地址可能会变,建议使用InetAddress.getLocalHost()方法获取本地地址。
作者:
希冀
时间:
2015-1-14 22:43
应该用UDP吧
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2