本帖最后由 黄业凌 于 2012-4-23 16:32 编辑
public class TCPSocketDemo {
/**
* TCP客户端
* 1.创建客户端服务的Socket服务,指定服务端ip,监听端口
* 2.获取socket流
* 3.OutputStream发送数据到服务端
* @param args
*/
public static void main(String[] args) throws IOException{
//创建Socket服务
Socket s = new Socket("191.168.1.101",10004);
System.out.println("TCP客户端启动成功");
//获取输出流
OutputStream out = s.getOutputStream();
//向服务端写入数据
out.write("你好".getBytes());
}
}
//运行Socket s = new Socket("191.168.1.101",10004); 报错 求解 端口未被占用 ip正确
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at cm.day5.netwrok.TCPSocketDemo.main(TCPSocketDemo.java:16) |