我试图通过TCP连接连接两个Android设备,我已经写了服务器和客户端代码....但是当我运行它时,它在屏幕上看起来什么也没有。我真的不知道自己做错了什么。 我在清单中提到了互联网许可 我检查了客人的IP地址 附:对不起,我的英语不好 客户代码:
package mybum.buff.client;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.net.*;
import java.io.*;
import android.view.View.*;
public class MainActivity extends Activity
{ private static final int TCP_PORT=21111;
private Button startcon, stopcon;
private TextView txt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startcon = (Button)findViewById(R.id.start);
stopcon = (Button)findViewById(R.id.stop);
Thread thread1 = new Thread(new Runnable(){
@Override
public void run()
{ try{
runserver();
}
catch(Exception e){}
}
});
thread1.start();
}
private void runserver(){
txt = (TextView)findViewById(R.id.text1);
try
{
Socket s = new Socket("192.168.43.148", TCP_PORT);
BufferedReader read =new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String outmsg ="Hi, I'm the client " +System.getProperty("line.separator");
write.write(outmsg);
write.flush();
String inmsg = read.readLine()+System.getProperty("line.separator")+;
txt.setText(inmsg);
s.close();
}
catch (IOException e)
{}
}
}
服务器代码: package blam.gbn.server;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.net.*;
import java.io.*;
import android.view.View.*;
public class MainActivity extends Activity
{ Button start, stop;
private TextView text1;
private ServerSocket ss;
private Socket s;
public static final int TCP_PORT=21111;
private BufferedReader read;
private BufferedWriter write;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button)findViewById(R.id.start);
stop = (Button)findViewById(R.id.stop);
text1 = (TextView)findViewById(R.id.text);
Thread thread1 = new Thread(new Runnable(){
@Override
public void run()
{
try{
runserver();
}
catch(Exception e){}
}
});
thread1.start();
}
private void runserver(){
ss=null;
try
{
ss = new ServerSocket(TCP_PORT);
text1.setText("2");
s = ss.accept();
text1.setText("3");
read = new BufferedReader(new InputStreamReader(s.getInputStream()));
write = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String inmsg = "Client : " + read.readLine()+ System.getProperty("line.separator");
text1.append(inmsg);
String outmsg = "Hi, I'm the server" + System.getProperty("line.separator");
write.write(outmsg);
write.flush();
s.close();
if (ss!=null){
ss.close();
}
}
catch (IOException e)
{}
}
}