我有一个服务器客户端代码,我从互联网上下载后修改为双向通信。我遇到了服务器数据接收错误。当客户端建立连接时,服务器通过向客户端发送“已连接到CLIENTIPADDRESS”来回复。
然后客户端发送“Hi server”消息。但是当服务器尝试读取收到的消息时,我收到Socket异常错误。
我为写入和读取服务器消息制作了单独的服务器线程。最初ByteArrayOutputStream用于阅读。它在我的情况下不起作用所以我使用BufferedReader,InputStreamReader和InputStream。我也尝试使用其他输入流读取类,但我得到了相同的套接字异常错误
SocketException是: 糟糕的套接字错误! java.net.SocketException:Socket已关闭。
当我在写入操作后检查插座是否关闭/打开时,插座仍然打开。
我不知道为什么IO流阅读器抛出异常。始终是第二个线程中的IO流缓冲区生成错误。据我所知,PrintStream不会抛出任何异常。因此,虽然客户端程序是相同的,它首先侦听然后发送数据,但数据接收线程之后的数据发送线程不会产生异常,因此数据被传输。
这里有什么问题?
public class MainActivity extends ActionBarActivity {
TextView Info, Infoip, Msg;
String message = "";
ServerSocket serverSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Info = (TextView) findViewById(R.id.info);
Infoip = (TextView) findViewById(R.id.infoip);
Msg = (TextView) findViewById(R.id.msg);
Infoip.setText(getIpAddress());
Thread socketServerThread = new Thread(new SocketServerThread());
socketServerThread.start();
}
protected void onDestroy() {
super.onDestroy();
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class SocketServerThread extends Thread{
static final int SocketServerPORT = 8080;
int count = 0;
Socket socket;
@Override
public void run() {
try {
serverSocket = new ServerSocket(SocketServerPORT);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Info.setText("I'm waiting here: "
+ serverSocket.getLocalPort());
}
});
while (true) {
socket = serverSocket.accept();
SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(socket);
socketServerReplyThread.start();
ServerReadThread serverReadThread = new ServerReadThread(socket);
serverReadThread.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ServerReadThread extends Thread {
InputStream is;
InputStreamReader isr;
String response = "";
BufferedReader br;
Socket hostSocket;
ServerReadThread(Socket socket){
hostSocket = socket;
}
public void run(){
try{
is = hostSocket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
response = br.readLine() + "\n";
is.close();
isr.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
message += "Oops Receiving error! " + e.toString() + "\n";
}
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Msg.setText(message);
}
});
}
}
private class SocketServerReplyThread extends Thread {
private Socket hostThreadSocket;
SocketServerReplyThread(Socket socket) {
hostThreadSocket = socket;
}
@Override
public void run() {
OutputStream outputStream;
String msgReply = "connected to " + hostThreadSocket.getInetAddress();
try {
outputStream = hostThreadSocket.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.print(msgReply);
printStream.close();
message += msgReply + "\n";
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Msg.setText(message);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "Something wrong! " + e.toString() + "\n";
}
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Msg.setText(message);
}
});
}
}
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "
+ inetAddress.getHostAddress() + "\n";
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
}
客户端套接字程序是:
public class ClientSocket extends Thread {
String serverAddress;
int serverPort;
int homeport;
Socket socket;
String response;
public ClientSocket(String addr, int port){
serverAddress = addr;
serverPort = port;
}
@Override
public void run(){ // make connection
try {
socket = new Socket(serverAddress, serverPort);
ClientReadThread clientReadThread = new ClientReadThread(socket); // client thread for reading
clientReadThread.run();
ClientWriteThread clientWriteThread = new ClientWriteThread(socket);
clientWriteThread.run();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
message = response;
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
textResponse.setText(message);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
message = response;
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
textResponse.setText(message);
}
});
} finally {
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
在服务器和客户端中,读写功能相似
答案 0 :(得分:0)
当您注释掉try
和catch
块时,代码不再编译,因此它不会执行,因此您当然不会获得任何例外,或者数据
你的问题没有意义。
但是,您的代码中还有其他几个错误:
start()
,而不是run()
。否则不会创建任何线程。read()
将阻止,直到数据可用。正如你所做的那样调用available()
方法是没有意义的,并且使用它作为消息结束或流结束的测试是不正确的:请参阅Javadoc。