如何将C ++套接字编程代码转换为C#?

时间:2015-03-30 14:43:54

标签: c# c++ sockets

伙计我不是专业人士,你们中的一些人可能认为这是一个基本问题,但我真的需要帮助,因为我正处于完成我的最后一年项目的最后期限。所以这就是我正在做的事情我发现两个代码是服务器和客户端,客户端通过某个指定的套接字将服务器发送到服务器。代码是用C ++编写的,适用于Linux。我正在寻找的是一种将代码的服务器端转换为C#的方法,以便我可以在Windows上运行它,客户端必须保留在C ++中才能在Linux上运行。这些代码都在下面用于引用。

服务器:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main ( int agrc, char *argv[] )
{
    /******** Program Variable Define & Initialize **********/
    int Main_Socket;    // Main Socket For Server
    int Communication_Socket; // Socket For Special Clients
    int Status; // Status Of Function
    struct sockaddr_in Server_Address; // Address Of Server
    struct sockaddr_in Client_Address;// Address Of Client That Communicate with Server
    int Port;
    char Buff[100] = "";
    Port = atoi(argv[2]);
    printf ("Server Communicating By Using Port %d\n", Port);
    /******** Create A Socket To Communicate With Server **********/
    Main_Socket = socket ( AF_INET, SOCK_STREAM, 0 );
    if ( Main_Socket == -1 )
    {
        printf ("Sorry System Can Not Create Socket!\n");
    }
    /******** Create A Address For Server To Communicate **********/
    Server_Address.sin_family = AF_INET;
    Server_Address.sin_port = htons(Port);
    Server_Address.sin_addr.s_addr = inet_addr(argv[1]);
    /******** Bind Address To Socket **********/
    Status = bind ( Main_Socket, (struct sockaddr*)&Server_Address, sizeof(Server_Address) );
    if ( Status == -1 )
    {
        printf ("Sorry System Can Not Bind Address to The Socket!\n");
    }
    /******** Listen To The Port to Any Connection **********/        
    listen (Main_Socket,12);    
    socklen_t Lenght = sizeof (Client_Address);
    int yx=1;
    char bs[10000] = "??";

    while (1)
    {
    Communication_Socket = accept ( Main_Socket, (struct sockaddr*)&Client_Address, &Lenght );

    if (!fork())
    {

        FILE *fp=fopen("recv.jpg","w");
        while(1)
        {
            char Buffer[10000]="";
            if (recv(Communication_Socket, Buffer, sizeof(Buffer), 0))
            {
                if ( strcmp (Buffer,bs) == 0  )
                {
                    break;
                }
                else
                {
                    fwrite(Buffer,sizeof(Buffer),1, fp);
            printf("\n%d) DATA RECIEVED", yx);
            yx=yx+1;
                }
            }
        }
        fclose(fp);
        send(Communication_Socket, "ACK" ,3,0);
        printf("\nACK Send\n");
        exit(0);
    }
    }
    return 0;
}

客户端:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main ( int agrc, char *argv[] )
{
    int Socket;

    struct sockaddr_in Server_Address;  
    Socket = socket ( AF_INET, SOCK_STREAM, 0 );
    if ( Socket == -1 )
    {   
    printf ("Can Not Create A Socket!");    
    }

    int Port ;
    Port = atoi(argv[2]);   

    Server_Address.sin_family = AF_INET;
    Server_Address.sin_port = htons ( Port );
    Server_Address.sin_addr.s_addr = inet_addr(argv[1]);

    if ( Server_Address.sin_addr.s_addr == INADDR_NONE )
    {
    printf ( "Bad Address!" );
    }   
    connect ( Socket, (struct sockaddr *)&Server_Address, sizeof (Server_Address) );


    FILE *in = fopen("a.jpg","r");
    char Buffer[10000] = "";
    char bs[10000] = "??";
    int len;
    int yx=1;

    while ((len = fread(Buffer,sizeof(Buffer),1, in)) > 0)
    {            
    send(Socket,Buffer,sizeof(Buffer),0);
    printf("\n %d) HELLO DOING IT", yx);
    yx = yx + 1;            
    }
    send(Socket,bs,sizeof(Buffer),0);

    char Buf[BUFSIZ];
    recv(Socket, Buf, BUFSIZ, 0);
    if ( strcmp (Buf,"ACK") == 0  )
    {
     printf("\nRecive ACK\n");
    }        
    close (Socket);
    fclose(in);

    return 0;   
}

P.S。这些都不是我自己的代码,因为我在互联网上发现它们,但它们工作得很好。

1 个答案:

答案 0 :(得分:1)

将套接字代码从C ++转换为C#非常简单。您可以通过近线逐行翻译快速完成。这并没有利用C#功能,但这很容易。

C#类Socketdocumentation)公开底层套接字API,您可以找到C ++ C#等效Socket()(构造函数),.Bind(),{{1 }},.Listen().Accept().Receive()

对于文件IO,您拥有.Send()File类。