一个简单的自定义通信协议(socket)

一直不知道socket通信时候自定义数据包是什么样子的,偶然做了个小例子。

 

先来说说数据包的定义,我这里是包头+内容 组成的:其中包头内容分为包类型+包长度, 那就是 消息对象=包类型+包长度+消息体

 

包类型 byte 型

包长度 int 型

消息体 byte[]

 

包总长度为 1 + 4 +  消息体.getBytes().length

 

发包方法如下:

private void sendTextMsg(DataOutputStream out,String msg ) throws IOException {
		byte[] bytes= msg.getBytes();
		int totalLen = 1 + 4 + bytes.length;
                                out.writeByte(1);
		out.writeInt(totalLen);
		out.write(bytes);
		out.flush();
	}

 

 

客户端发送消息类为:

 

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

public class MsgClient {

	private DataOutputStream outs;
	
	public static void main(String[] args) {
		try {
			MsgClient client = new MsgClient();
			client.connServer("127.0.0.1", 9292);			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	private void sendTextMsg(DataOutputStream out,String msg ) throws IOException {
		byte[] bytes= msg.getBytes();
		int totalLen = 1 + 4 + bytes.length;
        out.writeByte(1);
		out.writeInt(totalLen);
		out.write(bytes);
		out.flush();
	}	
	
	public void connServer(String ip,int port) throws UnknownHostException, IOException {
		Socket client = new Socket(ip,port);
		InputStream in = client.getInputStream();
		OutputStream out = client.getOutputStream();
		outs = new DataOutputStream(out);
		while(true) {
			Scanner scaner = new Scanner(System.in);
			sendTextMsg(outs, "测试消");
		}		
	}

 

 

服务端接收类为:

 

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MsgServer {
	public static void main(String[] args) {		
		try {
			MsgServer server = new MsgServer();
			server.setUpServer(9090);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setUpServer(int port) throws IOException {
		ServerSocket server = new ServerSocket(port);
		while(true) {
			Socket client = server.accept();
			System.out.println("客户端IP:"+client.getRemoteSocketAddress());
			processMesage(client);
		}
	}
	
	private void processMesage(Socket client) throws IOException {
		InputStream ins = client.getInputStream();		
		DataInputStream dins = new DataInputStream(ins);
		//服务端解包过程
		while(true) {
			int totalLen = dins.readInt();
			byte flag = dins.readByte();
			System.out.println("接收消息类型"+flag);
			
			byte[] data = new byte[totalLen - 4 - 1];
			dins.readFully(data);
			String msg = new String(data);
			System.out.println("发来的内容是:"+msg);	
		}
	}
}

 

 

 

这样就基本完成了,但实际还有好多问题,比如说服务端用如何用多线程服务来完成客户端的请求已提高效率,如果是NIO方式怎么来实现?多个消息类型时候怎么抽象?这些都没有考虑

 

另外有两个开源的框架不错,一个是apache  mina 还有个是netty ,有机会试试。

 

 

 

 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值