麻烦各位大牛帮帮忙 能将消息发送到服务端,但是不能接受到服务端发来的消息 客户端的代码如下(服务端是一个硬件模块(通过其他TCP软件测试无问题))
public class MainAppActivity extends Activity {
private static final String serverAddrSta ="192.168.11.254";//服务器端IP
private static final int port =8888;
private Socket socket = null;
private PrintWriter printWriter = null;
Button send = null;
Button conn = null;
EditText sendMes= null;
EditText rexMes= null;
private Thread thread = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String line = reader.readLine();
rexMes.setText("asdasd");
}
} catch (Exception e) {
Toast.makeText(MainAppActivity.this, "线程异常", Toast.LENGTH_SHORT).show();
}
}
};
public void CreateLink() throws IOException{
InetAddress serverAddr = InetAddress.getByName(serverAddrSta);//TCPServer.SERVERIP
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
Log.d("TCP", "C: Connecting...");
socket = new Socket(serverAddr, port);
thread.start();
}
public void write(String content){
try {
PrintWriter writer=new PrintWriter(socket.getOutputStream());
writer.println(content);
writer.flush();
Toast.makeText(MainAppActivity.this, "发送成功!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(MainAppActivity.this, "发送失败!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appmain);
Button send = (Button)this.findViewById(R.id.button1);
Button conn = (Button)this.findViewById(R.id.button2);
EditText sendMes= (EditText)this.findViewById(R.id.editText1);
EditText rexMes= (EditText)this.findViewById(R.id.rex);
try {
CreateLink(); //连接服务器
Toast.makeText(MainAppActivity.this, "连接服务器成功!", Toast.LENGTH_SHORT).show();
} catch (IOException e1) {
Toast.makeText(MainAppActivity.this, "连接服务器失败!", Toast.LENGTH_SHORT).show();
}
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
write("success"); //发送消息
} catch(Exception e) {
Log.e("TCP", "S: Error", e);
Toast.makeText(MainAppActivity.this, "连接服务器失败!", Toast.LENGTH_SHORT).show();
}
}
});
}
@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;
}
}
public class MainAppActivity extends Activity {
private static final String serverAddrSta ="192.168.11.254";//服务器端IP
private static final int port =8888;
private Socket socket = null;
private PrintWriter printWriter = null;
Button send = null;
Button conn = null;
EditText sendMes= null;
EditText rexMes= null;
private Thread thread = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
String line = reader.readLine();
rexMes.setText("asdasd");
}
} catch (Exception e) {
Toast.makeText(MainAppActivity.this, "线程异常", Toast.LENGTH_SHORT).show();
}
}
};
public void CreateLink() throws IOException{
InetAddress serverAddr = InetAddress.getByName(serverAddrSta);//TCPServer.SERVERIP
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
Log.d("TCP", "C: Connecting...");
socket = new Socket(serverAddr, port);
thread.start();
}
public void write(String content){
try {
PrintWriter writer=new PrintWriter(socket.getOutputStream());
writer.println(content);
writer.flush();
Toast.makeText(MainAppActivity.this, "发送成功!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(MainAppActivity.this, "发送失败!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appmain);
Button send = (Button)this.findViewById(R.id.button1);
Button conn = (Button)this.findViewById(R.id.button2);
EditText sendMes= (EditText)this.findViewById(R.id.editText1);
EditText rexMes= (EditText)this.findViewById(R.id.rex);
try {
CreateLink(); //连接服务器
Toast.makeText(MainAppActivity.this, "连接服务器成功!", Toast.LENGTH_SHORT).show();
} catch (IOException e1) {
Toast.makeText(MainAppActivity.this, "连接服务器失败!", Toast.LENGTH_SHORT).show();
}
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
write("success"); //发送消息
} catch(Exception e) {
Log.e("TCP", "S: Error", e);
Toast.makeText(MainAppActivity.this, "连接服务器失败!", Toast.LENGTH_SHORT).show();
}
}
});
}
@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;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货