Tuesday, February 25, 2014

Android socket communication problems, get help from friends


package com.example.chatactivity;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

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

public class MainActivity extends Activity implements Runnable {
private EditText ipaddr;
private EditText recvmsg;
private EditText sendmsg;
private Button send;
private Button start;
private Button stop;
private String ip;
private String sendtext;
private String recvtext;
Socket socket;
Thread thread;
DataInputStream in;
DataOutputStream out;

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
ipaddr = (EditText) findViewById (R.id.iptext);
recvmsg = (EditText) findViewById (R.id.recvtext);
sendmsg = (EditText) findViewById (R.id.sendtext);
send = (Button) findViewById (R.id.send);
start = (Button) findViewById (R.id.start);
stop = (Button) findViewById (R.id.stop);
stop.setEnabled (false);
send.setEnabled (false);
start.setOnClickListener (new View.OnClickListener () {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
ip = ipaddr.getText () toString ();.

thread = new Thread (MainActivity.this);
thread.start ();
/ / Start.setEnabled (false);
/ / Stop.setEnabled (true);
/ / Send.setEnabled (true);
}
});
send.setOnClickListener (new View.OnClickListener () {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
sendtext = sendmsg.getText () toString ();.
if (sendtext! = null) {
try {
out.writeUTF (sendtext);
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
}
});
stop.setOnClickListener (new View.OnClickListener () {

@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
try {
in.close ();
out.close ();
socket.close ();
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
stop.setEnabled (false);
start.setEnabled (true);
send.setEnabled (true);
}
});
}

@ 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 void run () {
/ / TODO Auto-generated method stub
try {
socket = new Socket (ip, 8000);
in = new DataInputStream (socket.getInputStream ());
out = new DataOutputStream (socket.getOutputStream ());
} Catch (UnknownHostException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
/ / Toast.makeText (this, "host name could not be resolved into an IP address", Toast.LENGTH_SHORT);
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
while (true) {
try {
recvtext = in.readUTF ();
mHandler.sendMessage (mHandler.obtainMessage ());
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
}
Handler mHandler = new Handler () {
public void handleMessage (Message msg) {
recvmsg.append (recvtext);
super.handleMessage (msg);
}
};

}
Always Rom server, and point start button the program in a virtual machine error stop<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Network operations in the sub-thread to do, not on the main UI thread.
Reply:
cited a floor tantahe reply:
network operating in the sub-thread to do, not on the main UI thread.

The main thread in just declare and define the socket number of variables, the new thread to accept it, so not okay
Reply:
reference to the second floor bdyz1016 reply:
Quote: references to a floor tantahe reply:

Network operations in the sub-thread to do, not on the main UI thread.

The main thread in just declare and define the socket number of variables, the new thread to accept it, so not okay

Wrong, sorry
Two suggestions:
1 The best debugging on a real machine.
2.3 edittext short pointer before using no judgment, easily lead to a null pointer exception. You look at the application hang is not the reason.
Reply:
reference to the third floor tantahe reply:
Quote: references to the second floor bdyz1016 reply:

Quote: references to a floor tantahe reply:

Network operations in the sub-thread to do, not on the main UI thread.

The main thread in just declare and define the socket number of variables, the new thread to accept it, so not okay

Wrong, sorry
Two suggestions:
1 The best debugging on a real machine.
2.3 edittext short pointer before using no judgment, easily lead to a null pointer exception. You look at the application hang is not the reason.


reference to the third floor tantahe reply:
Quote: references to the second floor bdyz1016 reply:

Quote: references to a floor tantahe reply:

Network operations in the sub-thread to do, not on the main UI thread.

The main thread in just declare and define the socket number of variables, the new thread to accept it, so not okay

Wrong, sorry
Two suggestions:
1 The best debugging on a real machine.
2.3 edittext short pointer before using no judgment, easily lead to a null pointer exception. You look at the application hang is not the reason.
I'm just learning , also does not see Logcat, I would first try is not a null pointer problem
Reply:

Reply:
debug will do
Reply:
reference to the 6th floor pengguohua1988 reply:
debug will do
less likely to debug, ipaddr filled after the IP address text box, click the start button, the program socket = new Socket (ip, 8000); execution unit anymore. Do not fill ipaddr text box, program execution to in = new DataInputStream (socket.getInputStream ()); do not go, socket is null
Reply:
Try udx agreement, http://www.goodudx.com/web/index.php/site/download/25, security desk Development Kit.
Reply:
 
socket = new Socket (ip, 8000);
in = new DataInputStream (socket.getInputStream ());
out = new DataOutputStream (socket.getOutputStream ());

This change
socket = new Socket ();
socket.connect (new InetSocketAddress (ip, 8000));
in = new DataInputStream (socket.getInputStream ());
out = new DataOutputStream (socket.getOutputStream ());
Then put this thread inside it.
while (true) {
try {
recvtext = in.readUTF ();
mHandler.sendMessage (mHandler.obtainMessage ());
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
This code has a loophole, if an exception that should exit the loop, reconnect socket

No comments:

Post a Comment