TCP Socket编程--客户端和服务端双向通信_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > TCP Socket编程--客户端和服务端双向通信

TCP Socket编程--客户端和服务端双向通信

 2010/12/11 11:31:52  谷超  http://gcguchao8888-sina-com.javaeye.com  我要评论(0)
  • 摘要:好久没有写javaeye博客了,最近写了个TCPSocket编程,和大家一起分享!服务端:packagetest;importjava.io.BufferedReader;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.ServerSocket;importjava.net
  • 标签:服务端 客户 客户端 服务 编程 socket

好久没有写javaeye博客了,最近写了个TCP Socket编程,和大家一起分享!

?

服务端

package test;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer2 {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
			new TCPServer2().go();
	}
	
	public void go(){
		ServerSocket ss;
		Socket s = null;
		try {
			ss = new ServerSocket(8888);
			s = ss.accept();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		SendThread send = new SendThread(s);
		new Thread(send).start();
		
		RecvThread recv = new RecvThread(s);
		new Thread(recv).start();
	}
	
	class SendThread implements Runnable{
		private Socket s = null;
		private DataOutputStream dos = null;
		
		public SendThread(Socket s){
			this.s = s;
			try {
				dos = new DataOutputStream(s.getOutputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		public void run() {
			String str = ""; 
			while(true){
				System.out.println("input message:");
				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
				try {
					str = br.readLine();
					dos.writeUTF(str);
					if("bye".equals(str))
						break;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	class RecvThread implements Runnable{
		private Socket s = null;
		private DataInputStream dis = null;
		
		public RecvThread(Socket s){
			this.s = s;
			try {
				dis = new DataInputStream(s.getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		public void run() {
			String str = "";
			while(true){
				try {
					str = dis.readUTF();
					if("bye".equals(str))
						break;
					System.out.println("received message:"+str);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}

?

客户端:

package test;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPClient2 {

		public static void main(String[] args) throws UnknownHostException, IOException{
			new TCPClient2().go();
		}
		
		public void go() {
			try {
				Socket s = new Socket("127.0.0.1",8888);
				RecvThread recv = new RecvThread(s);
				new Thread(recv).start();
				
				SendThread send = new SendThread(s);
				new Thread(send).start();
				} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		class RecvThread implements Runnable{
			private Socket s = null;
			private DataInputStream dis = null;
			
			public RecvThread(Socket s){
				this.s = s;
				try {
					dis = new DataInputStream(s.getInputStream());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void run() {
				String str = "";
				while(true){
					try {
						str = dis.readUTF();
						if("bye".equals(str))
							break;
						System.out.println("received message:"+str);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		
		class SendThread implements Runnable{
			private Socket s;
			private DataOutputStream dos;
			
			public SendThread(Socket s){
				this.s = s;
				try {
					dos = new DataOutputStream(s.getOutputStream());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void run() {
				String str = "";
				while(true){
					System.out.println("input message:");
					BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
					try {
						str = br.readLine();
						dos.writeUTF(str);
						if("bye".equals(str))
							break;
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		
}

?

发表评论
用户名: 匿名