客户端先建立连接后先写入输出流,然后再获得输入流。不然活有EOFException的异常。
不要忘记添加Internet权限,不要忘记添加Internet权限,不要忘记添加Internet权限
服务器端代码的编写:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
static ServerSocket aServerSocket = null; // Server Socet.
DataInputStream aDataInput = null; // Server input Stream that to
// receive msg from client.
DataOutputStream aDataOutput = null; // Server output Stream that to
static ArrayList list = new ArrayList();
public static void main(String[] args) {
try {
aServerSocket = new ServerSocket(50003); // listen 8888 port.
System.out.println("already listen 50003 port.");
} catch (Exception e) {
e.printStackTrace();
}
int num = 0;
while (num < 10) {
Socket aSessionSoket = null;
try {
aSessionSoket = aServerSocket.accept();
MyThread thread = new Server().new MyThread(aSessionSoket);
thread.start();
num = list.size();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class MyThread extends Thread {
Socket aSessionSoket = null;
public MyThread(Socket socket) {
aSessionSoket = socket;
}
public void run() {
try {
aDataInput = new DataInputStream(aSessionSoket.getInputStream());
aDataOutput = new DataOutputStream(aSessionSoket.getOutputStream());
list.add(aDataOutput);
while (true) {
String msg = aDataInput.readUTF(); // read msg.
if (!msg.equals("connect...")) {
System.out.println("ip: "+ aSessionSoket.getInetAddress());// ip.System.out.println("receive msg: " + msg);
for (int i = 0; i < list.size(); i++) {
DataOutputStream output = (DataOutputStream) list.get(i);
output.writeUTF(msg + "----" + list.size());
}
if (msg.equals("end"))
break;
}
aDataOutput.writeUTF("");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
aDataInput.close();
if (aDataOutput != null)
aDataOutput.close();
list.remove(aDataOutput);
aSessionSoket.close();
} catch (Exception e2) {
客户端代码的编写:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SocketActivity extends Activity {
EditText editText = null;
Button sendButton = null;
TextView display = null;
Socket client = null;
MyHandler myHandler;
DataOutputStream dout;
DataInputStream din;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clientsocket);
editText = (EditText) findViewById(R.id.message);
sendButton = (Button) findViewById(R.id.send);
display = (TextView) findViewById(R.id.display);
sendButton.setOnClickListener(listener);
try {
client = new Socket("192.168.0.120", 50003);
dout = new DataOutputStream(client.getOutputStream());
din = new DataInputStream(client.getInputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myHandler = new MyHandler();
MyThread m = new MyThread();
m.start();
}
class MyHandler extends Handler {
public MyHandler() {
}
// 子类必须重写此方法,接受数据
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.d("MyHandler", "handleMessage......");
super.handleMessage(msg);
// 此处可以更新UI
if (client != null && client.isConnected()) {
Log.i("handler..", "*-----*");
try {
dout.writeUTF("connect...");
String message = din.readUTF();
if (!message.equals(""))
display.setText(display.getText().toString() + " "+ "服务器发来的消息--:" + message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class MyThread extends Thread {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message msg = new Message();
SocketActivity.this.myHandler.sendMessage(msg);
}
}
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String sendText = editText.getText().toString();
try {
// din = new DataInputStream(client.getInputStream());
dout.writeUTF(sendText);
/*
* display.setText(display.getText().toString() + " " +
* "服务器发来的消息:" + din.readUTF());
*/
/*
* display.setText(display.getText().toString() + " " +
* "服务器发来的消息--:" + din.readUTF());
*/
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}