我有两个用C编写的程序。
服务器尝试发送一个360000字节的uint8_t
数组,客户端需要接收它。
我收到并发送了360000个字节,但由于某种原因,我没有按相同的顺序收到它们。 这两台机器都是小端的。
服务器 (接收是带发送的功能)
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), bind(), and connect() */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset*/
#include <unistd.h> /* for close*/
#include "HandleTCPClient.c"
#define MAXPENDING 5 /* Maximum outstanding connection requests */
void DieWithError(char *errorMessage); /* Error handling function */
void HandleTCPClient(int clntSocket); /* TCP client handling function */
int main(int argc, char *argv[])
{
int servSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
struct sockaddr_in ServAddr; /* Local address */
struct sockaddr_in ClntAddr; /* Client address */
unsigned short ServPort; /* Server port */
unsigned int clntLen; /* Length of client address data structure */
if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]) ;
exit(1);
}
ServPort = atoi(argv[1]); /* First arg: local port */
/* Create socket for incoming connections */
if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError( "socket () failed") ;
/* Construct local address structure */
memset(&ServAddr, 0, sizeof(ServAddr)); // Zero out structure
ServAddr.sin_family = AF_INET; // Internet address family
ServAddr.sin_addr.s_addr = htons(INADDR_ANY); // Any incoming interface
ServAddr.sin_port = htons(ServPort); // Local port
/* Bind to the local address */
if (bind(servSock, (struct sockaddr *)&ServAddr, sizeof(ServAddr)) < 0)
DieWithError ( "bind () failed");
// Mark the socket so it will listen for incoming connections
if (listen(servSock, MAXPENDING) < 0)
DieWithError("listen() failed") ;
int count = 0;
for (;;) // Run forever
{
count++;
/* Set the size of the in-out parameter */
clntLen = sizeof(ClntAddr);
/* Wait for a client to connect */
if ((clntSock = accept(servSock, (struct sockaddr *) &ClntAddr, &clntLen)) < 0)
DieWithError("accept() failed");
/* clntSock is connected to a client! */
printf ("%sConnection Number: %d :-)%s \n" ,"\033[1m\033[42m" , count , "\033[0m");
printf("Handling client %s\n", inet_ntoa(ClntAddr.sin_addr));
getchar();
HandleTCPClient (clntSock);
printf ("Finished Handling client %s\n", inet_ntoa(ClntAddr.sin_addr));
}
}
void DieWithError(char const *errorMessage)
{
printf ("%s" , "\033[1m\033[31m" ); // Bold RED
perror(errorMessage);
printf ("%s" , "\033[0m" ); // Black
exit(1);
}
SERVER - 调用处理TCP客户端(函数的主要部分)
array Data[6];
int totalRecvMsgSize = 0, recvMsgSize = 0;
printf ("RCVBUFSIZE:%d\n" , RCVBUFSIZE);
//printf("%sInside the recv%s\n" , "\033[1m\033[46m" , "\033[0m");
while((totalRecvMsgSize <= (RCVBUFSIZE - 1) ))
// we need a loop since recv retrives only the data curntley available.
{
//printf("%sInside the recv while loop%s\n" , "\033[1m\033[45m" , "\033[0m");
// Receive message from client
if ((recvMsgSize = recv(clntSocket, Buffer, RCVBUFSIZE - totalRecvMsgSize, 0)) < 0)
DieWithError("recv() failed") ;
else if (recvMsgSize == 0)
{
printf("(case is recvMsgSize == 0 ) ----> totalRecvMsgSize = %d\n", totalRecvMsgSize);
break;
}
else // if recvMsgSize > 0
totalRecvMsgSize += recvMsgSize;
}
Data[DataCounter].elementNumber = RCVBUFSIZE_SOURCE;
Data[DataCounter].colus = 1024; //step
Data[DataCounter].rows = 357; // rows
Data[DataCounter].key = DataCounter;
shiftData (Data);
客户端发送功能
void TcpClient ( char *servIP , unsigned short echoServPort , array_t *arrayData)
{
int i = 0;
for (i = 0 ; i < 365568 ; i++)
{
Data->arrayData[i] = i;
printf ("%d " ,Data->array[i]);
}
printf("\n");
//***************************************************************************************************************
//*****************************Create a socket and Establish connection to server *******************************
int sock; //Socket descriptor
struct sockaddr_in ServAddr; //Echo server address
int bytesRcvd, totalBytesRcvd; //Bytes read in single recv()
//and total bytes read
// Create a reliable, stream socket using TCP
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket () failed") ;
// Construct the server address structure
memset(&ServAddr, 0, sizeof(ServAddr)); /* Zero out structure */
ServAddr.sin_family = AF_INET; /* Internet address family */
ServAddr.sin_addr.s_addr = inet_addr(servIP);/* Server IP address */
ServAddr.sin_port = htons(echoServPort); /* Server port */
// Establish the connection to the server
if (connect(sock, (struct sockaddr *) &ServAddr, sizeof(ServAddr)) < 0)
DieWithError("connect () failed") ;
printf ("I am in Send\n");
size_t sent_total = 0;
int sent_now = 0;
sent_now = send(sockId, buffer + sent_total, len - sent_total, 0);
//printf("After sent(), sent now = %d\n" , sent_now);
if (sent_now == -1)
DieWithError("send() failed or connection closed prematurely");
printf ("send requested = %zu, sent = %zu\n", len, sent_total);
printf("\n"); // Print a final linefeed
我查看了手册页并在不同的论坛中找不到任何内容。 *我试图不放一切,因为它很长!