我如何处理我的套接字
How i can handle my socket
伙计们,当我 运行 它连接到服务器并发送消息时,我的代码有问题,但之后它停止了。任何建议
public void onClick(View arg0)
{ serverIP=Ip.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
try {
clientSocket= new Socket(serverIP,serverPort);//making the socket connection
printWriter=new PrintWriter(clientSocket.getOutputStream());
String connect="connected to server";
printWriter.write(connect);
printWriter.flush();
printWriter.close();
clientSocket.close();
Toast.makeText(getApplicationContext(),"Connected to:"+serverIP+" on port:"+serverPort,Toast.LENGTH_LONG).show();
} catch (UnknownHostException e) {
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}).start();
}
});
您不能在 运行nable 的 运行() 中调用 Toast()。
伙计们,当我 运行 它连接到服务器并发送消息时,我的代码有问题,但之后它停止了。任何建议
public void onClick(View arg0)
{ serverIP=Ip.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
try {
clientSocket= new Socket(serverIP,serverPort);//making the socket connection
printWriter=new PrintWriter(clientSocket.getOutputStream());
String connect="connected to server";
printWriter.write(connect);
printWriter.flush();
printWriter.close();
clientSocket.close();
Toast.makeText(getApplicationContext(),"Connected to:"+serverIP+" on port:"+serverPort,Toast.LENGTH_LONG).show();
} catch (UnknownHostException e) {
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}).start();
}
});
您不能在 运行nable 的 运行() 中调用 Toast()。