用java的Socket编写的小小小聊天工具,在本机调试成功,但是在两台能互相ping通的电脑却不能连接上
javasocket\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u5b9e\u73b0\u4e24\u53f0\u8ba1\u7b97\u673a\u95f4\u7684\u901a\u4fe1\u554a\u672c\u673a\u4f5c\u4e3a\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u65f6\u6ca1\u95ee\u9898\uff0c\u8bf7\u6559\u5404\u4f4d\u5927\u795e\u8c22\u8c22\u4f60\u6807\u9898\u8bf4\u4e24\u53f0\u8ba1\u7b97\u673a\uff0c\u4e3a\u4ec0\u4e48\u4f60\u7684client\u662fclient=new Socket("127.0.0.1",7080); \u662f\u672c\u673a\u5462\uff1f
\u8fd9\u4e2a\u8fd8\u662f\u81ea\u5df1\u505a\u6bd4\u8f83\u597d\u5427\uff01
建议先用一个最简单的程序测试客户端链接服务器,这样可以排除很多干扰,下面两个类已经是客户端和服务器的最简代码了,你先测一下,如果仍不能连接,把异常信息发上来看看。另:Socket连接容易出问题的地方:
1)端口号冲突,服务器无法启动;
2)IP地址错误,IP地址分本机地址、局域网地址和广域网地址,在不同的环境下使用不同的地址测试,如果错误将无法被客户端访问,还有如果机器安装了多网卡等问题都会引起错误;
3)防火墙禁止Java程序开放端口,不单是服务器机器的软防火墙,局域网的防火墙也可能阻止外部的访问。
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerMain {
public ServerMain() {
try {
ServerSocket ss = new ServerSocket(31647);
Socket s = ss.accept();
System.out.printf("客户端连接成功:\n%s\n", s.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ServerMain();
}
}
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientMain {
public ClientMain() {
try {
Socket s = new Socket("192.168.1.100", 31647); // 修改IP地址
System.out.println("连接服务器成功!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ClientMain();
}
}
是不是IP地址没做相应修改?
绛旓細寮鍙戣亰澶╁姛鑳藉彲浠ラ噰鐢ㄤ互涓嬪嚑绉嶆妧鏈細Socket缂栫▼锛浣跨敤Java Socket缂栫▼鍙互瀹炵幇鍩轰簬TCP鎴朥DP鐨勭綉缁滈氫俊锛岃繖鏄疛ava鏈鍩虹銆佹渶搴曞眰鐨勭綉缁滅紪绋嬫妧鏈備娇鐢⊿ocket缂栫▼鍙互瀹炵幇瀹炴椂閫氫俊銆佹秷鎭帹閫佺瓑鍔熻兘锛屼絾闇瑕佽嚜宸卞疄鐜版秷鎭崗璁佹暟鎹紶杈撶瓑缁嗚妭銆侸ava NIO锛欽ava NIO锛圢ew IO锛夋槸Java 1.4涔嬪悗寮曞叆鐨勪竴绉嶆柊IO API锛...
绛旓細package com.polyphylla.socket;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class SocketClient1 { private Socket socket;public SocketClient1() { try { so...
绛旓細瀹㈡埛绔殑銆係tring ip = "172.31.1.130";ip = "127.0.0.1";int port = 10004;try { Socket socket = new Socket(ip, port);socket.setSoTimeout(5539900);java.io.OutputStream out = socket.getOutputStream();byte[] date = "hello world".getBytes();out.write(data);out.flush(...
绛旓細鏈嶅姟绔 import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class MyServer implements Runnable{ private ServerSocket server;private static final int PORT = 8282;public MyServer(){ try { //寮鍚湇鍔 server = new ServerSocket(MyServer.PORT);} catch (IOException...
绛旓細鍒╃敤SOCKET閫氫俊鎶鏈紑鍙戜竴涓狢/S缁撴瀯鐨勭▼搴,瑕佹眰瀹㈡埛绔叿鏈夌櫥褰曢獙璇佺晫闈,鏈嶅姟鍣ㄧ閲囩敤澶氱嚎绋,鐢ㄦ埛鍛藉拰瀵嗙爜鐨勯獙璇佸湪鏈嶅姟鍣ㄧ鎵ц銆鐢ㄧ殑鏄痵wing鐨勬妧鏈.涓嶆槸web鐨.楂樺垎姹倊璧剁揣鍟.鎬ョ潃瑕... 鍒╃敤SOCKET閫氫俊鎶鏈紑鍙戜竴涓狢/S缁撴瀯鐨勭▼搴,瑕佹眰瀹㈡埛绔叿鏈夌櫥褰曢獙璇佺晫闈,鏈嶅姟鍣ㄧ閲囩敤澶氱嚎绋,鐢ㄦ埛鍛藉拰瀵嗙爜鐨勯獙璇佸湪鏈嶅姟鍣ㄧ...
绛旓細package socket;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class SocketService { //鎼缓鏈嶅姟鍣ㄧ public static void main(String[] args) throws IOExcepti...
绛旓細// 鏈嶅姟鍣 import java.io.*;import java.net.*;public class MyServer { public static void main(String[] args) throws IOException{ ServerSocket server=new ServerSocket(5678);while (true) { Socket client=server.accept();BufferedReader in=new BufferedReader(new InputStreamReader(client...
绛旓細鍩轰簬JAVA socket鐨鏈嶅姟鍣ㄥ鎴风缂栫▼ 鎴戣瀹炵幇涓涓郴缁:瀹㈡埛绔疉鍙互涓婁紶鏂囦欢缁欐湇鍔″櫒,浣嗗彧鏄笂浼犳枃浠跺悕,鏈満IP鍜岀鍙(鏈嶅姟鍣ㄥ彧鏄繚瀛樺鎴风鏂囦欢鐨勭储寮,骞朵笉淇濆瓨鏂囦欢鏈韩)~瀹㈡埛绔疉杩樺彲浠ュ埌鏈嶅姟鍣ㄤ笂鏌ョ湅鍏朵粬瀹㈡埛绔笂浼... 鎴戣瀹炵幇涓涓郴缁:瀹㈡埛绔疉鍙互涓婁紶鏂囦欢缁欐湇鍔″櫒,浣嗗彧鏄笂浼犳枃浠跺悕,鏈満IP鍜岀鍙(鏈嶅姟鍣...
绛旓細java缂栫▼瀵逛簬Socket涔嬮棿鐨勯氫俊杩囩▼濡備笅锛氭湇鍔$寰Socket鐨勮緭鍑烘祦閲岄潰鍐欎笢瑗匡紝瀹㈡埛绔氨鍙互閫氳繃Socket鐨勮緭鍏ユ祦璇诲彇瀵瑰簲鐨勫唴瀹广係ocket涓嶴ocket涔嬮棿鏄弻鍚戣繛閫氱殑锛屾墍浠ュ鎴风涔熷彲浠ュ線瀵瑰簲鐨凷ocket杈撳嚭娴侀噷闈㈠啓涓滆タ锛岀劧鍚庢湇鍔$瀵瑰簲鐨凷ocket鐨勮緭鍏ユ祦灏卞彲浠ヨ鍑哄搴旂殑鍐呭銆備笅闈㈡潵鐪嬩竴浜涙湇鍔$涓庡鎴风閫氫俊鐨勪緥瀛愶細publi...
绛旓細绗笁绉嶅姙娉曪紝灏卞湪灞鍩熺綉閲屾悶銆傛妸瀹㈡埛绔殑璁剧疆鍐欐垚浣犵殑鏈嶅姟绔墍鍦ㄦ満鍣ㄧ殑鍐呯綉IP锛屼箣鍚庡彧瑕佸湪涓涓唴缃戠殑瀹㈡埛绔氨鍙互浜掔浉浜ゆ祦浜嗐備互涓婂墠鎻愭槸锛屼綘鎶浣跨敤鍒扮殑鏈哄櫒闃茬伀澧欏叧浜嗐侾S锛氭垜鏇剧粡涔熷啓杩嘠Q锛屼笉杩囨槸鏈嶅姟绔拰瀹㈡埛绔泦鎴愬湪涓璧凤紝涓斾负灞鍩熺綉妯″紡銆備篃灏辨槸璇达紝鍙鏈夋煇涓汉鍦ㄥ眬鍩熺綉涓惎鍔ㄤ簡璇Q锛屼綘灏变細...