[問題] Socket傳送訊息
請教版上各位高手
我在寫TCPClient的時候發現了一個很奇怪的問題
再送訊息的時候 明明是同樣的程式碼(我將傳送訊息寫成一個method)
可是在不同的區域底下 一個Server端收的到訊息
另一個從外部呼叫的 Server卻收不到
這是什麼問題呢??? 有請版上各位幫我解惑了
我抓了這個蟲抓了快一天.....
程式碼如下
public Boolean connectServer( String ServerIp )throws Exception{
System.out.println("Connecting To Server... ");
InetAddress IP = InetAddress.getByName(ServerIp);
Client = new Socket ( IP , Port );
if(!Client.isConnected()){
assert Client.isConnected()==false;
System.out.println("Connection failed");
}
//會成功送到Server端的片段
try{
sendToServer("Message");
}
catch(Exception e){
}
return Client.isConnected();
}
//Send message to TCP Server
private void sendToServer(String Message){
//Assertion for TCP connection
if(!Client.isConnected()){
assert Client.isConnected();
System.out.println("Connection Failed");
}else{
System.out.println("Connection work");
}
try{
DataOutputStream outToServer = new
DataOutputStream(Client.getOutputStream());
outToServer.writeBytes(Message+ "Client 1 \n");
System.out.println("Sending '"+Message+"'");
}catch(IOException e){
}
}
.
.
.
.
.
.
public static void main(String args[])
{
try{
TCPClient test = new TCPClient();
test.connectServer("localhost")
//這行Server端卻收不到
test.inputMoves("Fail");
}catch(Exception e){
System.out.println(e);
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.115.217.47
※ 編輯: a39189 來自: 140.115.217.47 (04/22 21:33)
→
04/22 21:41, , 1F
04/22 21:41, 1F
→
04/22 21:42, , 2F
04/22 21:42, 2F
推
05/06 19:50, , 3F
05/06 19:50, 3F
討論串 (同標題文章)