0

Guys I am a not a pro and some of you might think its a basic question but I really need help as I am on deadline to complete my Final Year Project. So here is what I am doing I found two codes a server and a client in which client sends Images to the server over some specified socket. The code is written in C++ and is for Linux. What I am looking for is a way to convert Server side of the code to C# so that I can run it on Windows and client must remain in C++ to be run on Linux. Both these codes are below for referencing.

Server:

#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;
}

Client:

#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. None of these are my own codes they are as I found them on the Internet but they are working perfectly.

3
  • 1
    If you're able to find example C++ code I'm pretty sure you can find the same of C# Commented Mar 30, 2015 at 14:47
  • Do you really need it to be C# ? The C++ code will work as is on windows. Commented Mar 30, 2015 at 14:50
  • Yeah thats the thing I have already worked on the interface and several other modules using C# and it would be a tiresome process to convert it all to C++ as I am on a tight schedule. :( Commented Mar 30, 2015 at 14:57

1 Answer 1

1

Translating socket code from C++ to C# is quite straightforward. Your can do it quite quickly with a near line by line translation. That doesn't take advantage of C# features, but that's easy.

The C# class Socket (documentation) expose the underlying socket API, and you can find the C++ C# equivalent Socket() (constructor), .Bind(), .Listen(), .Accept(), .Receive(), .Send().

For the file IO, you have the File and FileStream classes.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi there! Thank for guiding. It was helpful but now have one more problem. While playing with codes I was able to successfully send a string from Windows C# to Linux C++ but so far unable to do it other way around. Can you please help with this error that I am getting. System.Net.Sockets.SocketException (0x80004005): A request to send or receive data was disallowed because the socket is not connected.
@Xtreme_Enigma That's hard to tell without code, but as the message says, the socket is probably not connected, or not anymore. Also, maybe you are using the wrong socket on the server side ? Remember that the server has two sockets : one server socket and one client socket. Only the client socket should be used for recv/send.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.