我的任務是通過來自客戶端的TCP連接接收數據,然後將相同的值連續發送回客戶端。這是一個基本代碼,但我只需確定p1 [i]是否與p [i]具有相同的值,並將接收到的數據發送回客戶端,因爲我無法在明天之前對其進行測試。我剛剛把通信部分包含在客戶端,而不是連接建立。通過TCP接收和發送數據
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s , new_socket;
struct sockaddr_in server , client;
int c;
char iResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
char sendbuf [DEFAULT_BUFLEN];
int sendbuflen = DEFAULT_BUFLEN;
unsigned int i;
uint8_t* p;
uint8_t* p1;
int x=0;
int q=0;
size_t len;
p = (uint8_t*)recvbuf;
len= iResult/sizeof(uint8_t);
do
{
iResult = recv(new_socket, recvbuf, recvbuflen, 0);
{
for(i=0; i<len; i++)
{
p[i];
}
}
if(iResult == 0)
{
wprintf(L"Connection closed\n");
}
else
{
wprintf(L"recv failed with error: %d\n", WSAGetLastError());
}
}
while(iResult > 0);
// Send uint8_t back data to client
p1 = (uint8_t*)sendbuf;
iResult = send(new_socket, sendbuf, sendbuflen, 0);
{
for(q=0; q<len; q++)
{
p1[q] = p[q];
}
}
return 0;
closesocket(new_socket);
WSACleanup();
return 0;
}
而且你的問題是什麼? – EJP
代碼是否將收到的數據發送回去? –