简体   繁体   English

如何通过套接字连接发送不同的数据类型

[英]How to send different datatypes over in a socket connection

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. 这可以表示一个字符串,字符或字符串数​​组。

If you want to send this all in one packet, you need to have designated length for each type of data structure, ie the string has a max of 1024 bytes, and the bytes have a max of 512 bytes...etc. 如果要全部发送到一个数据包中,则需要为每种类型的数据结构指定长度,即字符串的最大长度为1024个字节,而字节的最大长度为512个字节...等等。 Doing this will let you be able to decipher the information on the receiving end. 这样做将使您能够在接收端解密信息。

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; 如果采用此路由,则需要指定数据包的第一个字节以标记接收方这是什么类型的数据。 ie 1=bytes, 2=string, 3=array..etc 即1 =字节,2 =字符串,3 =数组..etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM