如何在套接字连接中发送不同的数据类型 [英] How to send different datatypes over in a socket connection

查看:111
本文介绍了如何在套接字连接中发送不同的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个客户端/套接字程序,该程序涉及从客户端发送字符串(以识别应该使用哪个密钥)到服务器,服务器以字节形式发回密钥,然后客户端发送一个请求文件以字符串形式,服务器将请求的文件发送给用户,该文件可以用密钥解密。
我理解密码学方面,我很想知道如何区分向服务器发送字节,字符串或文件。我理解如何发送单个流(字节,字符串或文件),但如果有意义的话,找不到在一个流中发送所有这些的方法?
每次我想发送一个字符串时,是否必须创建一个新的流或套接字连接,然后一个新的发送字节,然后一个新的发送一个文件?

I'm trying to make a client/socket program that involves sending a string from the client (to identify which key should be used) to the server, the server sends back a key in byte form, the client then sends a request for a file in string form, the server sends the requested file to the user which can be decrypted with they key. I understand the cryptology aspects, I'm hung up on how to differentiate between sending bytes, string or a file to and from a server. I understand how to send a single stream (bytes, string or a file), but cannot find a way to send all of these in one stream if that makes any sense? Do I have to create a new stream or socket connection each time I want to send a string, then a new one to send bytes, then a new one to send a file?

我可以查找任何资源吗?干杯!

Any resources I could perhaps look up? Cheers!

推荐答案

基本上,通过套接字连接发送的是一堆字节。这可以表示字符串,字符或字符串数​​组。等等。

Basically, what's sent over a socket connection is a bunch of bytes. This can represent a string, character, or an array of strings..etc.

如果要在一个数据包中发送全部数据,则需要为每种类型的数据结构指定长度,即字符串最多为1024字节,并且字节最多有512个字节......等等。这样做可以让您解密接收端的信息。

If you want to send this all in one packet, you need to have designated length for each type of data structure, i.e. the string has a max of 1024 bytes, and the bytes have a max of 512 bytes...etc. Doing this will let you be able to decipher the information on the receiving end.

如果您没有最大尺寸而又不想设置它们,那么您可以采用不同的方法并将每个数据结构发送到自己的数据包中。如果采用这种方法,则需要指定数据包的第一个字节,以便向接收器标记这是什么类型的数据;即1 =字节,2 =字符串,3 = array..etc

If you don't have maximum size and don't want to set them then you can take a different approach and send each data structure in its own packet. If you take this route you will need to designate the first byte of the packet to flag the receiver what type of data this is; i.e. 1=bytes, 2=string, 3=array..etc

这篇关于如何在套接字连接中发送不同的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆