今天来写一个使用Socket通信的小小实例
实现效果:快看动图
效果说明:当点击发送时,是先将文本内容发送到本地服务器,之后再从服务器中获取显示到TextView中的
PC服务端实现:
package com.lollo.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
/*
* 服务端的任务:
* 1:负责接收每一个客户端传来的数据
* 2: 将读取到的客户端的数据发送给 其他每一个客户端(包括自己)
*/
public class SimpleServerList {
public static ArrayList<Socket>socketList=new ArrayList();
public static void main(String[]args) throws IOException{
final ServerSocket server=new ServerSocket(8090);
System.out.println("服务器已建立:"+server);
//时刻监听
new Thread(){
public void run(){
while(true){
try{
Socket socket=server.accept();
socketList.add(socket);
ServerThread serverThread=new ServerThread(socket);
serverThread.start();
}catch (IOException e){
//System.out.println("服务端移除socket");
//socketList.remove(socket);
}
}
}
}.start();
}
private static class ServerThread extends Thread{
Socket socket;
//负责读取客户端的数据
DataInputStream input;
DataOutputStream output;
public ServerThread(Socket socket) throws IOException{
this.socket=socket;
input=new DataInputStream(socket.getInputStream());
}
//读取客户端数据并发送给每一个其他客户端
public void run(){
String content=null;
//if((content=readFromClient())!=null){
while((content=readFromClient())!=null){
//遍历所有客户端的socket
for(Iterator<Socket> it=SimpleServerList.socketList.iterator();it.hasNext();){
try{
Socket socket=it.next();
output=new DataOutputStream(socket.getOutputStream());
//发送给其他客户端
output.writeUTF(content+"\n");
}catch(IOException e){
}
}
}
}
private String readFromClient(){
String readStr=null;
try {
readStr=input.readUTF();
} catch (IOException e) {
System.out.println("移除socket:");
SimpleServerList.socketList.remove(socket);
}
return readStr;
}
}
}
Android客户端的实现:
package com.lollo.server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
/*
* 服务端的任务:
* 1:负责接收每一个客户端传来的数据
* 2: 将读取到的客户端的数据发送给 其他每一个客户端(包括自己)
*/
public class SimpleServerList {
public static ArrayList<Socket>socketList=new ArrayList();
public static void main(String[]args) throws IOException{
final ServerSocket server=new ServerSocket(8090);
System.out.println("服务器已建立:"+server);
//时刻监听
new Thread(){
public void run(){
while(true){
try{
Socket socket=server.accept();
socketList.add(socket);
ServerThread serverThread=new ServerThread(socket);
serverThread.start();
}catch (IOException e){
//System.out.println("服务端移除socket");
//socketList.remove(socket);
}
}
}
}.start();
}
private static class ServerThread extends Thread{
Socket socket;
//负责读取客户端的数据
DataInputStream input;
DataOutputStream output;
public ServerThread(Socket socket) throws IOException{
this.socket=socket;
input=new DataInputStream(socket.getInputStream());
}
//读取客户端数据并发送给每一个其他客户端
public void run(){
String content=null;
//if((content=readFromClient())!=null){
while((content=readFromClient())!=null){
//遍历所有客户端的socket
for(Iterator<Socket> it=SimpleServerList.socketList.iterator();it.hasNext();){
try{
Socket socket=it.next();
output=new DataOutputStream(socket.getOutputStream());
//发送给其他客户端
output.writeUTF(content+"\n");
}catch(IOException e){
System.out.println("Iterator移除socket:");
it.remove();
System.out.println(SimpleServerList.socketList);
//SimpleServerList.socketList.remove(socket);
}
}
}
}
private String readFromClient(){
String readStr=null;
try {
readStr=input.readUTF();
} catch (IOException e) {
System.out.println("移除socket:");
SimpleServerList.socketList.remove(socket);
}
return readStr;
}
}
}
今天先写到这儿,后续更新