0

服务器

我认为这一面还可以,但我说我会为了确定而把它放上去。

import java.io.*;
import java.net.*;
public class Server_wifi {
    public static void main(String[] args) throws Exception {
        ServerSocket sersock = new ServerSocket(3000);
        System.out.println("Server  ready for chatting");
        Socket sock = sersock.accept( );                          
        // reading from keyboard (keyRead object)
        BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
        // sending to client (pwrite object)
        OutputStream ostream = sock.getOutputStream(); 
        PrintWriter pwrite = new PrintWriter(ostream, true);

        // receiving from server ( receiveRead  object)
        InputStream istream = sock.getInputStream();
        BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

        String receiveMessage, sendMessage;               
        while(true) {
            if((receiveMessage = receiveRead.readLine( )) != null) {
                System.out.println(receiveMessage);         
            }         
            sendMessage = keyRead.readLine(); 
            pwrite.println(sendMessage);             
            System.out.flush();                              
        }               
    }                    
}

客户

我认为问题出在客户端,但似乎无法真正弄清楚在哪里。

package com.example.testprogram;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private Socket server;
    private PrintWriter printwriter;
    private EditText textField;
    private EditText textField2;
    private Button button;
    private String messsage;
    private int x  =0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textField = (EditText) findViewById(R.id.tf1); //reference to the text field
        textField2 = (EditText) findViewById(R.id.tf2); //reference to the text field
        button = (Button) findViewById(R.id.wifi);   //reference to the send button
        textField.setText("Started... "); 
        //Button press event listener
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                x++;   
                messsage = textField.getText().toString(); //get the text message on the text field
                textField.setText("");      //Reset the text field to blank
                System.out.println("Got here...");
                try {

                    Socket sock = new Socket("172.16.71.175", 3000);
                    // reading from keyboard (keyRead object)
                    BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
                    // sending to client (pwrite object)
                    OutputStream ostream = sock.getOutputStream(); 
                    PrintWriter pwrite = new PrintWriter(ostream, true);

                    // receiving from server ( receiveRead  object)
                    InputStream istream = sock.getInputStream();
                    BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

                    textField.setText("Start the chitchat, type and press Enter key");

                    String receiveMessage, sendMessage;
                    while(true) {
                        sendMessage = textField.getText().toString();     // keyboard reading
                        pwrite.println(sendMessage);       // sending to server
                        System.out.flush();         // flush the data

                        if((receiveMessage = receiveRead.readLine()) != null) //receive from server
                        {
                            textField.setText(receiveMessage); // displaying at DOS prompt
                        }         
                    }               
                }catch (UnknownHostException e) {
             textField.setText("Unknown host");
                 e.printStackTrace();
                 } catch (IOException e) {
             textField.setText("IO exception"+e.toString());
                 e.printStackTrace();
                 }                                   
            }
        });

    }
}

它似乎正在使我的应用程序崩溃,它也一直告诉我我错过了 } 来完成。我正在尝试在电话和计算机之间建立一个双向系统,请提供帮助。我提出了一个建议,比如建议让应用程序一直没有响应。

4

1 回答 1

0

您应该在客户端和服务器端调用pwrite.flush()而不是System.out.flush()在调用之后调用。pwrite.println(sendMessage)您正在刷新错误的输出流。

于 2013-01-25T22:51:44.087 回答