java socket回显服务

根据java tcp/ip编程学习记录  java socket实现回显服务
逻辑比较简单直接代码记录
1.客户端
package com.pjf.echodemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class EchoClient {

	public static void main(String[] args) {
		Socket socket = null;
		InputStream sockIn = null;// 读取
		OutputStream sockOut = null;// 输出流

		try {
			socket = new Socket("localhost", 6666);
			/**
			 * 获取网络输入输出流的实例
			 */
			sockIn = socket.getInputStream();
			sockOut = socket.getOutputStream();

			// 从控制台读取
			Scanner console = new Scanner(System.in);
			String context = null;// 控制台去读内容
//			byte[] readBytes = new byte[64];
			int length=0;
			while (true) {
				context = console.nextLine();
//				限制输入内容大小
//				if(context.getBytes().length>64){
//					System.out.println("输入数据过长无法发送,请缩短内容");
//					continue;
//				}
				length=context.getBytes().length;
				sockOut.write(context.getBytes());
				sockOut.flush();
				
				//sockIn.read(readBytes, 0, context.getBytes().length);
				int rec_len=0;//收到返回消息的长度
				int rec_total_len=0;//收到返回消息的总长度
				byte[] rev_data=new byte[length];
				//读取判断,防止服务端拆分
				while(rec_total_len<length){
					rec_len=sockIn.read(rev_data,rec_total_len,length-rec_total_len);
					if(rec_len==-1){
						break;
					}
					rec_total_len+=rec_len;
					
				}
				System.out.println("客户端:"+new String(rev_data, 0, length));
				if (context == null || context == "" || context.equalsIgnoreCase("exit")) {
					break;
				}

			}
			System.out.println("结束了,goodbye!!!");

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (socket != null) {
				if (!socket.isClosed()) {
					try {
						socket.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				socket = null;

			}

			if (sockIn != null) {

				socket = null;

			}

			if (sockOut != null) {

				sockOut = null;

			}

		}

	}

}


2.服务端端:
package com.pjf.echodemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {

	public static void main(String[] args) {
		ServerSocket ss = null;
		Socket socket = null;
		InputStream inStream = null;
		OutputStream outStream = null;

		try {
			ss = new ServerSocket(6666);
			System.out.println("服务端启动成功=================================");
			while (true) {
				socket = ss.accept();
				System.out.println(
						"客户端接收成功" + socket.getLocalAddress().getHostName() + "=================================");
				byte[] revdata = new byte[16];
				inStream = socket.getInputStream();
				outStream = socket.getOutputStream();

				int len = 0;
				//没有关闭连接则一直读取
				while ((len = inStream.read(revdata)) != -1) {

					System.out.println("服务端收到:" + new String(revdata, 0, len));
					outStream.write(revdata, 0, len);

				}

				if (outStream != null) {
					outStream = null;
				}

				if (inStream != null) {
					inStream = null;
				}

				if (socket != null) {
					if (!socket.isClosed()) {
						try {
							socket.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}

		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			if (ss != null) {
				if (!ss.isClosed()) {
					try {
						ss.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}

	}
}



3.由于客户端知道发送的长度,可以等待服务端返回这些内容,进行相应的打印,服务端不知道长度,在服务打印可能都是分开,这个后面可能通过定长编码或者分隔符进行解决,或者每次发送限度不长度


4.存在以下问题:只能够单个socket访问,读写相互堵塞...


5.上面的问题根据后面的章节可以补充....

猜你喜欢

转载自pengjianf-ah.iteye.com/blog/2405759