English 中文(简体)
TCP/IP转移期间腐败的数据
原标题:Socket data corrupted during TCP/IP transfer
  • 时间:2010-11-23 19:00:52
  •  标签:
  • c
  • sockets

在发送有关与前联网的TCP-IP的袖珍数据时,我看到数据正被腐蚀。

Example:

1号站正在向2号站发送数据。 我在发送(S1)之前和接收(S2)之后都印制了数据。 以下信息:

S1: Data sent is ACK
S2: Data received is AC�����

不能确定问题是什么。 在发送数据(S1)和接收数据(S2)之前,我甚至已经清除了char。

Any hint/info for the above would be of great help.

最佳回答

这通常是以下一些因素的结果:

/* BAD CODE */
const char* ack = "ACK";
err = write( sockfd, ack, strlen( ack )); /* sender */
/* ... */
char buf[SOME_SIZE]
readb = read( sockfd, buf, SOME_SIZE ); /* receiver */
printf( "%s", buf );

上述法典的问题在于,投稿人只写了三(3)份书。 这不包括扼杀零装置。 然后,接收人拿到数据,要么根本不核对系统报到的回报价值,要么/要么盲目印刷所收到数据。 <代码>f将印刷所有材料,直至其发现以零价值表示的记忆。

Edit:

根据你的评论,我认为你假设以下一点:send(2) over TCP socket should result in one select(2)>和朋友——这是一个单独议题)。

两个公认的申请级议定书设计是:

  • Communicate via pre-defined fixed-length messages - this way you read until you get that many bytes from the socket. Simple.
  • Include message type and/or message length into the message itself - this is usually done with fixed-length message header that is followed by varying message payload. Read until you get full header then switch/dispatch/continue reading depending on type/length.

Don t forget about endianess - networks like network byte order.

问题回答

您是否保证收到全部信息? 由于TCP是一个基于上游的议定书,你在2号站上读到的数据可以输入数据的小丘。 你们必须审视一下一下一下一下一下一下一下一下你在缓冲中的职能有多少数据。

For example, your first call to recv() can come up with "AC" and then, the next call may give you the rest of the data "K"

在客户方面,我向以下人员发出了一份插图:

char message[200];

/*string to be sent*/
strcpy(message, "Hi PQRS, How are you!?");

/*send string to server s socket*/
if( send(socket_desc , message , strlen(message)+1 , 0) < 0)
    {
        puts("Send failed");
        return 1;
    }
puts("Data Sent
");

通知

斯特伦(主题)+ 1

to allow for the delimiter i.e. instead of

斯特伦(主题)

它确保了扼杀的结束,没有额外数据。

无论服务器方面的性质阵列大小,只要其等于或大于所架设的座标,就可以做到这一点。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签