一个Socket应用示例_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 一个Socket应用示例

一个Socket应用示例

 2011/10/6 8:11:48  xchao  http://xchao.iteye.com  我要评论(0)
  • 摘要:近日一个项目需要所其它项目进行集成,需要用Socket进行通讯,于是简单的学习了Socket编程方面的知识,做了个小应用示例,在此记录下,以增加印象,应用是比较简单,但从了解Socket到顺序跑通这个应用,还是花了点时间的,至少搞明白了如下几个问题:1、从远程端(Server里面的Client、Client里面的Server)Socket的输入流(InputStream)中读取数据(一般用BufferedReader),从输入流(OutputStream)中写入数据
  • 标签:一个 应用 socket
近日一个项目需要所其它项目进行集成,需要用Socket进行通讯,于是简单的学习了Socket编程方面的知识,做了个小应用示例,在此记录下,以增加印象,应用是比较简单,但从了解Socket到顺序跑通这个应用,还是花了点时间的,至少搞明白了如下几个问题:



1、从远程端(Server里面的Client、Client里面的Server)Socket的输入流(InputStream)中读取数据(一般用BufferedReader),从输入流(OutputStream)中写入数据(一般用PringWriter)

2、在使用BufferedReader的readLine()方法时,会造成当前线程的阻塞,直到能从InputStream读取到数据才继续往下走,所以如果在持续的从InputStream中读取(类似如下代码

while((line = reader.readLine()) != null){
//...
}


)时,一定要在最后发送一个结束标识,以告知对话结束。

3、



完整的代码如下:

SocketServer:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author xchao
 *
 */
public class SocketServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			ServerSocket server = new ServerSocket(10086);
			while(true){
				Socket client = server.accept();
				System.out.println("a client(" + client.getInetAddress().getHostAddress() + ") is coming.");
				SocketHandlerThread clientThread = new SocketHandlerThread(client);
				clientThread.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
class SocketHandlerThread extends Thread{
	private Socket client = null;
	public SocketHandlerThread(Socket client){
		this.client = client;
	}
	public void run(){
		try {
			try{
				InputStream inStream = client.getInputStream();
				OutputStream outStream  = client.getOutputStream();
				PrintWriter out = new PrintWriter(outStream, true);
				out.println("Hello! Please enter BYE to exit.");
				BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
				String line = null;
				boolean eof = false;
				while(!eof && (line = in.readLine()) != null){
					System.out.println("Client msg:" + line);
					out.println(line + "(reply from server)");
					if(line.trim().equals("BYE")){
						out.println("BYEBYE");
						eof = true;
					}
				}
				out.close();
				in.close();
			}finally{
				client.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}






SocketClient:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author xcaho
 *
 */
public class SocketClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Socket server = new Socket(InetAddress.getLocalHost(), 10086);
			try{
				OutputStream outStream = server.getOutputStream();
				PrintWriter out = new PrintWriter(outStream, true);
				BufferedReader readerSysIn = new BufferedReader(new InputStreamReader(System.in));
				String readLine = null;
				while(!((readLine = readerSysIn.readLine()).equals("BYE"))){
					out.println(readLine);
				}
				out.println("BYE");
				
				String line = null;
				BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream()));
				boolean eof = false;
				while(!eof && (line = reader.readLine()) != null){
					System.out.println(line);
					if(line.trim().equals("BYEBYE")){
						eof = true;
					}
				}
				out.close();
				reader.close();
			}finally{
				server.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}







发表评论
用户名: 匿名