TCP IP之间的连接时,Windows的C多线程

时间:2019-06-25 06:40:09

标签: c windows multithreading visual-studio

我希望我能解释。我是C编程的新手,并尝试使用TCPIP发送和接收二进制文件。客户端发送文件时,服务器应一次接收多个文件。我读取了一个bat文件以发送到服务器。如果文件是2或3,则没有问题,但是尝试发送大约5个文件时,有时会显示错误。实际上文件未正确接收。我用

  

Semaphore方法的多线程同步

打印时接收方(服务器)的结果如下:

file name (5000.dat)
Invalid argumen(5000.dat) 
completetfile name (5120.dat)
(5120.dat) complete
file name (8192.dat)
(8192.dat) complete
file name (10240.dat)
(10240.dat) complete

上面放错了一些文字,每次显示不同的结果。有时会正确显示和写入文件,有时会无法读取某些文件。

我接收方的代码如下:

#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <process.h>
#include <windows.h>
void fileReceive(void *param);
HANDLE semaphore;
HANDLE threadHandle;
  int main(int argc, char *argv[]){
     if (argc > 1) {
         goto l_param_error;
     }
     WSADATA wsaData; // Contains information about the Windows Sockets implementation
     SOCKET sock0; // creates a socket that is bound to a specific transport    service provider.
     struct sockaddr_in addr;
     struct sockaddr_in client;
     int len;
     SOCKET sock; // creates a socket that is bound to a specific transport service provider

    // Initiates use of the Winsock DLL by a process.
     int error = WSAStartup(MAKEWORD(2, 2), &wsaData);
     if (error != 0) {
         goto l_WSAIni_error;
     }
     addr.sin_family = AF_INET;// internetwork: UDP, TCP, etc.
     addr.sin_port = htons(8080);
     addr.sin_addr.S_un.S_addr = INADDR_ANY;
     sock0 = socket(AF_INET, SOCK_STREAM, 0);
        if (sock0 == INVALID_SOCKET) {
            goto l_socket_error;
        }
        // associates a local address with a socket
        if (bind(sock0, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
            goto l_bind_error;
        }

        while (1) {

            // places a socket in a state in which it is listening for an incoming connection
            if (listen(sock0, 1) != 0) {
                goto l_socket_conn_setup_error;
            }

            len = sizeof(client);
            // The accept function permits an incoming connection attempt on a socket.
            sock = accept(sock0, (struct sockaddr *)&client, &len);
            if (sock == INVALID_SOCKET) {
                goto l_error_accpet;
            }
            semaphore = CreateSemaphore(0, 1, 1, 0);

            threadHandle = (HANDLE)_beginthread(&fileReceive, 0, &sock);
            if (threadHandle == 0) {
                printf("Thread handle error");
                return 1;
            }
            CloseHandle(semaphore);
        }

        WSACleanup();
        return 0;
    }

void fileReceive(void *param) {
    int n = 0;
    int sock = *((int *)param);
    unsigned char buf[1];
    unsigned char buff[256] = { 0 };
    FILE *fp = NULL;
    memset(buff, 0, sizeof(buff));
    WaitForSingleObject(semaphore, INFINITE);
    // Receive file name
    int recvFile = recv(sock, buff, 255, 0);
    ReleaseSemaphore(semaphore, 1, 0);
    if ((recvFile == 0) || (recvFile == -1)) {
        goto l_recv_error;
    }
    fp = fopen(buff, "wb+");
    if (fp != NULL) {

        printf("file name (%s)\n", buff);

        while (n = recv(sock, &buf[0], 1, 0) > 0) {

            size_t written = fwrite(&buf, sizeof(buf), 1, fp);

            if (written != 1) {
                goto l_write_error;
            }
        }
        printf("(%s) complete\n", buff);

    }
    else {
        goto l_fp_error;
    }

    fclose(fp);
    closesocket(sock);
    _endthread();
    CloseHandle(threadHandle);
}

1 个答案:

答案 0 :(得分:4)

不幸的是,您有一堆问题。坦率地说,您似乎不了解TCP(这是一种字节流协议),而且您似乎也不了解线程同步解决了哪些问题以及如何使用它。鉴于此,您正在尝试的任务超出了您的能力,应该首先尝试更简单的任务。从不使用线程的TCP代码或不使用TCP的线程代码开始,这样您就不必一次完成所有操作。

以下是一些问题:

  1. 您将&sock传递给线程。但是然后可能在线程可以读取它之前更改sock的值。

  2. 您在TCP连接上调用recv以获取文件名,并假设您将读取所有(且仅)文件名。 TCP无法将字节“粘合”在一起成为一条消息。如果要发送和接收消息,则必须定义消息协议并在TCP之上实现。

  3. 您的信号灯实际上什么也没做。您不使用它来交流或同步任何东西。

  4. 每次读取1时,您写入256个字节。