编写程序实现多次通信 (socket和多线程)

服务端

<span style="font-size:18px;"><span style="font-size:18px;">
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务端
 * @author Administrator
 *
 */
public class Server {

	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(8989);
		System.out.println("---------服务器已启动-------------");
		while(true){
			Socket socket = server.accept();
			new ThreadSocket(socket).start();//把每次侦听到的socket,传给一个专门处理socket的线程类
		}

	}

}
</span></span>

客户端

<span style="font-size:18px;"><span style="font-size:18px;">
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 客户端
 * @author Administrator
 *
 */
public class Client {

	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket socket = new Socket("localhost",8989);
		System.out.println("-----客户端已启动-------");
		new ThreadReader(socket.getInputStream()).start();
		new ThreadWriter(socket.getOutputStream()).start();

	}

}
</span></span>

ThreadSocket

<span style="font-size:18px;"><span style="font-size:18px;">
import java.io.IOException;
import java.net.Socket;

/**
 * 服务器处理socket的线程类
 * @author Administrator
 *
 */
public class ThreadSocket extends Thread {
	
	private Socket socket;

	public ThreadSocket(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		
		try {
			ThreadReader tr = new ThreadReader(socket.getInputStream());
			ThreadWriter tw = new ThreadWriter(socket.getOutputStream());
			tr.start();
			tw.start();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}
</span></span>
ThreadReader
<span style="font-size:18px;"><span style="font-size:18px;">
import java.io.IOException;
import java.io.InputStream;

/**
 * 读取数据的线程类
 * @author Administrator
 *
 */
public class ThreadReader extends Thread {
	
	private InputStream is;

	public ThreadReader(InputStream is) {
		this.is = is;
	}

	@Override
	public void run() {
		
		try {
			
			while(true){
				byte[] b = new byte[1024];
				int len = is.read(b);
				String s = new String(b,0,len);
				System.out.println(s);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
</span></span>
threadWriter
<span style="font-size:18px;"><span style="font-size:18px;">
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;

/**
 * 发送数据的线程类
 * @author Administrator
 *
 */
public class ThreadWriter extends Thread {

	private OutputStream os;
	Scanner sc;
	
	public ThreadWriter(OutputStream os) {
		this.os = os;
		this.sc = new Scanner(System.in);
	}

	@Override
	public void run() {
		
		
		try {
			while(true){
				System.out.println("请发送信息:");
				String info = sc.next();
				os.write(info.getBytes());
				os.flush();//刷新
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(os!=null){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
}
</span></span>




阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭