建立本地socket连接,客户端向服务器发送个字符串,服务器接收并返回int port=134;
Socket s;
try {
s = new Socket("10.26.71.25", port);//建立本地socket连接
OutputStream out=s.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
dout.writeUTF("tom"); //写入输入流
InputStream in =s.getInputStream();
DataInputStream din=new DataInputStream(in);
String string=din.readUTF(); //从输入流中读数据
System.out.println(string);
in.close();
out.close();
s.close();
} catch (UnknownHostException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}无法建立连接...我在学校里,用锐捷上网,每台机器有个固定IP地址
s = new Socket("10.26.71.25", port);10.26.71.25是我的内网IP地址,这样写好像无法建立连接,参数应该写什么呢谢各位了...
Socket s;
try {
s = new Socket("10.26.71.25", port);//建立本地socket连接
OutputStream out=s.getOutputStream();
DataOutputStream dout=new DataOutputStream(out);
dout.writeUTF("tom"); //写入输入流
InputStream in =s.getInputStream();
DataInputStream din=new DataInputStream(in);
String string=din.readUTF(); //从输入流中读数据
System.out.println(string);
in.close();
out.close();
s.close();
} catch (UnknownHostException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}无法建立连接...我在学校里,用锐捷上网,每台机器有个固定IP地址
s = new Socket("10.26.71.25", port);10.26.71.25是我的内网IP地址,这样写好像无法建立连接,参数应该写什么呢谢各位了...
另外,java写socket一般不需要这么复杂,建议使用rmi或者开源的rpc的实现。用java而不会利用java社区开源资源是非常可悲的。
程序员专属T恤:http://t4coder.us
s = new Socket("localhost",3456);
s = new Socket(InetAddress.getLocalHost(), 5678);
等等 试了很多端口号还是错误un:
2010-5-9 18:30:21 socketInfo.Main main
严重: null
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)