Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我做错了什么:Android套接字连接?_Java_Android_Sockets - Fatal编程技术网

Java 我做错了什么:Android套接字连接?

Java 我做错了什么:Android套接字连接?,java,android,sockets,Java,Android,Sockets,我正在将一个从Thenebston.com学到的客户端-服务器应用程序转换为Android应用程序,它是一个即时消息应用程序,使用套接字(TCP连接)进行通信,在我的Android应用程序中,一个设备是服务器,另一个是客户端。现在,在emulator上运行服务器端应用程序时出现问题,应用程序冻结约40秒,然后弹出一条错误消息,说明: Activity ServerIM(应用程序中的Android服务器即时消息) 他没有反应 我认为问题在于创建ServerSocket。我真的需要这个,所以我很感激

我正在将一个从Thenebston.com学到的客户端-服务器应用程序转换为Android应用程序,它是一个即时消息应用程序,使用套接字(TCP连接)进行通信,在我的Android应用程序中,一个设备是服务器,另一个是客户端。现在,在emulator上运行服务器端应用程序时出现问题,应用程序冻结约40秒,然后弹出一条错误消息,说明:

Activity ServerIM(应用程序中的Android服务器即时消息) 他没有反应

我认为问题在于创建ServerSocket。我真的需要这个,所以我很感激任何人都能帮我度过难关

这是我的
ServeIM
Android类:

package com.example.android.server.instant.messenger;

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class ServerIM extends Activity {

    private ScrollView scrollView;
    private EditText userText;
    private Button connectButton;
    private Button sendButton;
    private TextView chatLog;

    private ObjectOutputStream output;
    private ObjectInputStream input;
    private ServerSocket server;
    private Socket connection;

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

        scrollView = (ScrollView) findViewById(R.id.ScrollView);
        userText = (EditText) findViewById(R.id.user_Text);
        connectButton = (Button) findViewById(R.id.button_connect);
        sendButton = (Button) findViewById(R.id.button_send);
        chatLog = (TextView) findViewById(R.id.chat_Log);

        ableToType(false);

        connectButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showMessage("1"); // this message doesn't appends to the
                                    // TextView
                startRunning();
            }
        });

    }

    public void startRunning() {
        try {
            (new Thread(new Runnable() {
                public void run() {
                    try {
                        server = new ServerSocket(5678, 100);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            })).start();
            while (true) {
                try {
                    waitForConnection();
                    setupStreams();
                    whileChatting();
                } catch (EOFException eofException) {
                    showMessage("\n Server ended the connection!");
                } finally {
                    closeCrap();
                }
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

    }

    // wait for connection, then display connection information
    private void waitForConnection() throws IOException {
        showMessage("Waiting for someone to connect... \n");
        connection = server.accept();
        showMessage("Now connected to "
                + connection.getInetAddress().getHostName());
    }

    // get stream to send and recieve data
    private void setupStreams() throws IOException {
        output = new ObjectOutputStream(connection.getOutputStream());
        output.flush();
        input = new ObjectInputStream(connection.getInputStream());
        showMessage("\n Streams are now setup! \n");
    }

    // during the chat conversation
    private void whileChatting() throws IOException {
        String message = " You are now connected! ";
        sendMessage(message);
        ableToType(true);
        do {
            // have a conversation
            try {
                message = (String) input.readObject();
                showMessage("\n" + message);
            } catch (ClassNotFoundException classNotFoundException) {
                showMessage("\n idk wtf that user sent! ");
            }
        } while (!message.equals("CLIENT - END"));
    }

    // close streams ans sockets after you are done chatting
    private void closeCrap() {
        showMessage("\n Closing Connections... \n");
        ableToType(false);
        try {
            output.close();
            input.close();
            connection.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    // send message to client
    private void sendMessage(String message) {
        try {
            output.writeObject("SERVER - " + message);
            output.flush();
            showMessage("\n Server - " + message);
        } catch (IOException ioException) {
            chatLog.append("\n ERROR: I can't send this message");
        }
    }

    // update chatWindow
    private void showMessage(final String text) {
        final Handler myHandler = new Handler();

        (new Thread(new Runnable() {
            public void run() {
                myHandler.post(new Runnable() {
                    public void run() {
                        chatLog.append(text + "\n");
                    }
                });
            }
        })).start();
    }

    // let the user type stuff into their box
    private void ableToType(final boolean tof) {

        final Handler myHandler = new Handler();

        (new Thread(new Runnable() {
            public void run() {
                myHandler.post(new Runnable() {
                    public void run() {
                        userText.setEnabled(tof);
                    }
                });
            }
        })).start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_server_im, menu);
        return true;
    }
}
这是XML文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/ScrollView"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <TextView
            android:id="@+id/chat_Log"
            android:layout_width="wrap_content"
            android:layout_height="339dp"
            android:text="" />
    </ScrollView>

    <EditText
        android:inputType="text"
        android:id="@+id/user_Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/ScrollView"
        android:layout_marginTop="39dp"
        android:ems="10" />

    <Button
        android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/user_Text"
        android:layout_toRightOf="@+id/user_Text"
        android:minWidth="100dip"
        android:text="Send" />

    <Button
        android:id="@+id/button_connect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button_send"
        android:layout_alignRight="@+id/button_send"
        android:layout_below="@+id/button_send"
        android:text="Connect" />

</RelativeLayout>

以及舱单:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.server.instant.messenger"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ServerIM"
            android:label="@string/title_activity_server_im" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

用户界面线程中正在运行
ServerSocket.accept()
函数,该函数将被阻止,直到获得连接为止


您的想法是正确的,在另一个线程中创建了
ServerSocket
,但是您必须在后台运行
accept()
函数。

ServerSocket.accept()
函数在UI线程中运行,并将阻塞,直到它获得连接


您的想法是正确的,在不同的线程中创建了
ServerSocket
,但是您必须在后台运行
accept()
函数。

在事件处理程序(ServerIM.java:68)中有
while(true)
,这使得EDT挂起。

在事件处理程序(ServerIM.java:68)中有
while(true)
,这使EDT挂起。

请尝试找到您问题的最小示例请尝试找到您问题的最小示例