本帖最后由 steven152 于 2014-8-22 16:12 编辑
以下是我照着毕老师敲的代码,结果报异常: java.net.ConnectException: Connection refused: connect
请问下大神是什么原因,该怎么改?
import java.io.*;
import java.net.*;
class TcpClient2 {
public static void main(String [] args) throws Exception
{
Socket s = new Socket("192.168.1.137",1005);
OutputStream out = s.getOutputStream();
out.write("服务端,你好!".getBytes());
InputStream in = s.getInputStream();
byte [] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String (buf,0,len));
s.close();
}
}
class TcpServer2 {
public static void main(String [] args)throws Exception
{
ServerSocket ss = new ServerSocket (10005);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"...connected");
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
OutputStream out = s.getOutputStream();
out.write("哥们收到,你也好".getBytes());
s.close();
ss.close();
}
}
|
|