我对Android编程非常陌生。
我在android中的代码如下
public class AndroidClient extends Activity {
EditText textOut;
TextView textIn;
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_client);
textOut = (EditText)findViewById(R.id.textout);
Button buttonSend = (Button)findViewById(R.id.send);
textIn = (TextView)findViewById(R.id.textin);
buttonSend.setOnClickListener(buttonSendOnClickListener);
}
Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new BackgroundDataTask().execute("");
}};
public class BackgroundDataTask extends AsyncTask<String,String,String> {
private Exception ex;
@Override
protected String doInBackground(String... urls) {
try {
socket = new Socket("10.20.50.68", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(textOut.getText().toString());
textIn.setText(dataInputStream.readUTF());
socket = serverSocket.accept();
textIn.setText(socket.getInetAddress().toString());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return "";
}
}
}我在C#代码中有一个服务器,代码侦听端口并写入输出
但是,这只是在我第一次输入文本并单击发送时,在服务器端接收到代码,我可以在控制台中看到ouptput,但在此之后没有接收到该值- .So -如何保持这个套接字运行?我遗漏了什么?
谢谢大家
发布于 2013-08-17 06:12:24
尝尝这个
创建一个类client.java
public class client{
Socket socket;
int Port;
InetAddress serverIp;
public boolean connection;
public client(String ip, int port) {
// TODO Auto-generated constructor stub
try {
serverIp = InetAddress.getByName(ip);
Port = port;
connect();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
connection = false;
}
}
public void connect(){
try {
System.out.println("wait connection client "+serverIp+" "+Port);
socket = new Socket(serverIp, Port);
connection = true;
} catch (IOException e) {
//e.printStackTrace();
connection = false;
}
}
public boolean send(String message) throws IOException {
byte[] data=message.getBytes();
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
int len = data.length;
dos.writeInt(len);
if (len > 0) {
dos.write(data, 0, len);
}
return true;
}
public String read() {
InputStream in = null;
try {
in = socket.getInputStream();
} catch (IOException e) {
connection=false;
e.printStackTrace();
}
DataInputStream dis = new DataInputStream(in);
try {
int len = dis.readInt();
byte[] data = new byte[len];
if (len > 0) {
dis.readFully(data);
}
String result = new String(data, 0, data.length);
return result;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
connection=false;
}
return "";
}并在活动client.java中创建onCreate对象。
client con=new client("10.20.50.68", 8888);用con.send("your_data")发送,String data=con.read();接收数据.如果希望异步侦听套接字中的数据,请使用线程。
https://stackoverflow.com/questions/18285409
复制相似问题