簡單的Socket通信實例:實現Android客戶端與PC服務端的簡單通信

今天來寫一個使用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;
	}
 }
}

今天先寫到這兒,後續更新

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章