Reputation: 3205
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.
Upvotes: 0
Views: 1058
Reputation: 6145
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.
Upvotes: 1