1

我是 java TCP 套接字的新手。我试图实现一个服务器和一个客户端。所以服务器应该检查输入(做某事)并将字符串发送给客户端。客户端应该将字符串发送到服务器并从服务器查找输入字符串(并做一些事情)。如果有新东西可用,两者都应该一直循环检查和发送。客户端可以向服务器发送数据,服务器接收它并可以显示/处理此数据。但是客户端不显示来自服务器的数据。有人能告诉我为什么客户端没有从服务器接收到字符串吗?有更好的想法来做无限循环吗?将只有一个客户端和一个服务器。

 while true:
 server out------>  send String----->   in client            
        in<-----  sent String <------   out

这是简化的服务器部分:

public class MainActivity extends Activity {
Socket client;
ServerSocket server;
int serverport = 54321;
String inputData = null;
BufferedReader in;
PrintWriter out;
String outputData;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(setupConnection).start();    
}

private Runnable setupConnection = new Thread() {
public void run() {
try {
server = new ServerSocket(serverport);
    while (true) {
    client = server.accept();
    in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    out = new PrintWriter(client.getOutputStream(), true);
    inputData = in.readLine();
    InputStream inStream = new ByteArrayInputStream(inputData.getBytes());
    in.close();
        if (inputData != null) {
        System.out.println(TAG + "-----Incoming Message---- " + inputData);
        //this is working String is shown           
        }       }
    out.write("nothing to do?");
    out.flush();
    out.close();
    }

} catch (SocketException e) {
Log.v(TAG, "SocketException: " + e);
e.printStackTrace();
} catch (IOException e) {
Log.v(TAG, "IOException: " + e);
e.printStackTrace();
}
}

简化的客户端如下所示:

public class testClass {
public static void main(String[] args) throws IOException, InterruptedException {
Socket socket = null;
String host = "127.0.0.1";
int port = 54321;
PrintWriter out = null;
BufferedReader in = null;

while (true) {
try {
    socket = new Socket(host, port);
    out = new PrintWriter(socket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
    System.out.println(TAG + "Error: " + e);
    System.err.println("Don't know about host: localhost.");
    System.exit(1);
} catch (IOException e) {
    System.out.println(TAG + "Error: " + e);
    System.err.println("Couldn't get I/O for " + "the connection to: localhost.");
    System.exit(1);
}

out.println("Hello, is it me you're looking for...");
out.flush();


String input = in.readLine();
System.out.println("Input: " + input);

in.close();
out.close();

}
}
}
4

2 回答 2

2

如果 readLine() 返回 null,则对端已关闭连接,您也必须这样做。并停止阅读。

于 2013-04-26T23:38:01.517 回答
2

如果您想在 android 中实现此代码,您将面临很多问题:您可以在此链接中找到解决方案: Post JSON in android

在以下代码中可能会解决此问题:

    HttpPost post = new HttpPost("http://xxxxxx");
    DefaultHttpClient httpClient = new DefaultHttpClient();
    post.setEntity(new ByteArrayEntity(json.toString().getBytes()));
    HttpResponse response = httpClient.execute(post);
    return EntityUtils.toString(response.getEntity());

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    System.out.println(">>>>>>>" + e.getMessage());
} catch (ClientProtocolException e) {
    e.printStackTrace();
    System.out.println(">>>>>>>" + e.getMessage());
} catch (IOException e) {
    e.printStackTrace();
    System.out.println(">>>>>>>" + e.getMessage());
}
于 2013-04-26T23:45:04.077 回答