我在linux上制作服务器。而客户端是android。 android客户端和linux服务器在同一个本地网络下。 但linux服务器无法捕获客户端的连接。 我的代码有什么问题?
(客户端)
package com.example.hello;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.net.InetAddress;
import java.net.InetAddress;
import java.net.InetAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.LinkedList;
public class MainActivity extends ActionBarActivity {
private Button Btn_Connect;
private static final int ERROR = -1;
private static final int DATA = 2;
private static final String ERROR_KEY = "_error";
private static final String DATA_KEY = "_data";
private static int port = 8888;
LinkedList<SocketClient> threadList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
threadList = new LinkedList<MainActivity.SocketClient>();
Btn_Connect = (Button) findViewById(R.id.Btn_cennect);
Btn_Connect.setOnClickListener(new OnClickListener() {
Socket socket = null;
@Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(), "asasdfsdf", 100).show();
Log.d("msg", "park0");
SocketClient client = new SocketClient("192.168.171.129");
threadList.add(client);
client.start();
Intent intent = new Intent(MainActivity.this, Connected.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class SocketClient extends Thread{
boolean threadAlive;
String ip;
public SocketClient(String ip){
threadAlive = true;
this.ip = ip;
}
@Override
public void run() {
Socket socket = null;
InputStream inputStream = null;
BufferedInputStream bufStream = null;
try {
socket = new Socket(ip,port);
inputStream = socket.getInputStream();
// bufStream = new BufferedInputStream(inputStream);
int len = 0;
byte[] readBytes = new byte[1000];
while( (len = bufStream.read(readBytes)) != -1 ){
// Message msg = Message.obtain();
// msg.what = DATA;
// Bundle bundle = new Bundle();
// bundle.putString(DATA_KEY, new String(readBytes, 0, len));
// msg.setData(bundle);
// uiHandler.sendMessage(msg);
}
} catch (Exception e) {
// Message msg = Message.obtain();
// msg.what = ERROR;
// Bundle bundle = new Bundle();
// bundle.putString(ERROR_KEY, e.toString());
// msg.setData(bundle);
// uiHandler.sendMessage(msg);
} finally{
try {
bufStream.close();
inputStream.close();
socket.close();
} catch (IOException e) {
// Message msg = Message.obtain();
// msg.what = ERROR;
// Bundle bundle = new Bundle();
// bundle.putString(ERROR_KEY, e.toString());
// msg.setData(bundle);
// uiHandler.sendMessage(msg);
}
}
}
}
}
答案 0 :(得分:0)
public class ChatApplication extends Application {
private Socket mSocket;
{
try {
mSocket = IO.socket(Constants.CHAT_SERVER_URL);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
public Socket getSocket() {
return mSocket;
}
}
这将返回套接字。