サーバ
こちら側は大丈夫だと思いますが、念のため載せると言いました。
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();
}
}
});
}
}
アプリがクラッシュしているようにも見えますが、完了するには}が不足していると言い続けます。ここで電話とコンピューターの間で双方向システムを取得しようとしています。助けてください。アプリが応答しなくなることを提案するようなキャッチを入れました。