我刚写了一个基于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();
- }
- }
复制代码
}
客户端和服务器端不能进行正常通信,是不是输入流的问题?求指点。 |
|