Created
September 17, 2016 00:03
-
-
Save define-private-public/bcb0ed407d5b6fa83e69aced3e41eb7d to your computer and use it in GitHub Desktop.
Porting a C/Berkeley Sockets application to C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Filename: client.c | |
// Author: Benjamin N. Summerton <define-private-public> | |
// License: Unlicense (https://unlicense.org/) | |
// | |
// Adapted From: | |
// https://en.wikibooks.org/wiki/C_Programming/Networking_in_UNIX | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
const int MAX_RECV_LEN = 255; | |
const int PORT_NUM = 6000; | |
// Main method | |
int main(int argc, char *argv[]) { | |
char buffer[MAX_RECV_LEN + 1]; | |
int len, clientSocket; | |
struct sockaddr_in serv; | |
// Create a TCP/IP socket | |
clientSocket = socket(AF_INET, SOCK_STREAM, 0); | |
memset(&serv, 0, sizeof(serv)); | |
serv.sin_family = AF_INET; | |
serv.sin_addr.s_addr = htonl(INADDR_LOOPBACK); // 127.0.0.1 (a.k.a. localhost) | |
serv.sin_port = htons(PORT_NUM); | |
// Connect to the server | |
printf("Connecting to the server...\n"); | |
connect(clientSocket, (struct sockaddr *)&serv, sizeof(struct sockaddr)); | |
// Get a message (blocks) | |
len = recv(clientSocket, buffer, MAX_RECV_LEN, 0); | |
buffer[len] = '\0'; // Null terminate the string | |
printf("Got a message from the server [%i bytes]:\n%s", len, buffer); | |
// cleanup | |
close(clientSocket); | |
return 0; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Filename: Makefile | |
# Author: Benjamin N. Summerton <define-private-public> | |
# License: Unlicense (https://unlicense.org/) | |
# Change the value you're preferred c compiler (e.g. `clang` or `gcc`) | |
CC=clang | |
all: server client | |
server: server.c | |
$(CC) -o server server.c | |
client: client.c | |
$(CC) -o client client.c | |
clean: | |
rm server | |
rm client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Filename: server.c | |
// Author: Benjamin N. Summerton <define-private-public> | |
// License: Unlicense (https://unlicense.org/) | |
// | |
// Adapted From: | |
// https://en.wikibooks.org/wiki/C_Programming/Networking_in_UNIX | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <signal.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <netinet/in.h> | |
const int PORT_NUM = 6000; | |
int running = 0; | |
int serverSocket = 0; // Socket to listen for incoming connections | |
// SIGINT handler | |
void sigintHandler(int sig) { | |
if (sig == SIGINT) { | |
printf("Received SIGINT, shutting down server.\n"); | |
// Cleanup | |
running = 0; | |
if (serverSocket) | |
close(serverSocket); | |
// end program | |
exit(0); | |
} | |
} | |
// Main Method | |
int main(int argc, char *argv[]) { | |
char *msg = "Hello, Client!\n"; | |
struct sockaddr_in dest; // socket info about remote machine | |
struct sockaddr_in serv; // socket info about us | |
int clientSocket; // Socket FD for the remote client | |
socklen_t socksize = sizeof(struct sockaddr_in); | |
// Init and create the socket | |
memset(&serv, 0, sizeof(serv)); // Zero out struct before filling | |
serv.sin_family = AF_INET; // Mark as TCP/IP | |
serv.sin_addr.s_addr = htonl(INADDR_ANY); // Put it on any interface | |
serv.sin_port = htons(PORT_NUM); // Set server port number | |
serverSocket = socket(AF_INET, SOCK_STREAM, 0); | |
// Bind serv information to the socket | |
bind(serverSocket, (struct sockaddr *)&serv, sizeof(struct sockaddr)); | |
// Start listening for new connections (queue of 5 max) | |
listen(serverSocket, 5); | |
// Setup SIGINT handler | |
if (signal(SIGINT, sigintHandler) != SIG_ERR) { | |
running = 1; | |
printf("Running the TCP server.\n"); | |
} | |
// Main loop | |
while (running) { | |
// Wait for a new client (blocks) | |
clientSocket = accept(serverSocket, (struct sockaddr *)&dest, &socksize); | |
// print some info about the remote client | |
printf("Incoming connection from %s, replying.\n", inet_ntoa(dest.sin_addr)); | |
// Send a reply (blocks) | |
send(clientSocket, msg, strlen(msg), 0); | |
// Close the connection | |
close(clientSocket); | |
} | |
return 0; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Filename: TcpSocketClientExample.cs | |
// Author: Benjamin N. Summerton <define-private-public> | |
// License: Unlicense (https://unlicense.org/) | |
// | |
// Adapted & Ported From: | |
// https://en.wikibooks.org/wiki/C_Programming/Networking_in_UNIX | |
using System; | |
using System.Text; | |
using System.Net; | |
using System.Net.Sockets; | |
namespace Client | |
{ | |
class TcpSocketClientExample | |
{ | |
public static int MaxReceiveLength = 255; | |
public static int PortNumber = 6000; | |
// Main method | |
public static void Main(string[] args) | |
{ | |
int len; | |
byte[] buffer = new byte[MaxReceiveLength + 1]; | |
// Create a TCP/IP Socket | |
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
IPEndPoint serv = new IPEndPoint(IPAddress.Loopback, PortNumber); | |
// Connect to the server | |
Console.WriteLine("Connecting to the server..."); | |
clientSocket.Connect(serv); | |
// Get a message (blocks) | |
len = clientSocket.Receive(buffer); | |
Console.Write("Got a message from the server[{0} bytes]:\n{1}", | |
len, Encoding.ASCII.GetString(buffer, 0, len)); | |
// Cleanup | |
clientSocket.Close(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Filename: TcpSocketServerExample.cs | |
// Author: Benjamin N. Summerton <define-private-public> | |
// License: Unlicense (https://unlicense.org/) | |
// | |
// Adapted & Ported From: | |
// https://en.wikibooks.org/wiki/C_Programming/Networking_in_UNIX | |
using System; | |
using System.Text; | |
using System.Net; | |
using System.Net.Sockets; | |
namespace Server | |
{ | |
class TcpSocketServerExample | |
{ | |
public static int PortNumber = 6000; | |
public static bool Running = false; | |
public static Socket ServerSocket; | |
// An interrupt handler for Ctrl-C presses | |
public static void InterruptHandler(object sender, ConsoleCancelEventArgs args) | |
{ | |
Console.WriteLine("Received SIGINT, shutting down server."); | |
// Cleanup | |
Running = false; | |
ServerSocket.Shutdown(SocketShutdown.Both); | |
ServerSocket.Close(); | |
} | |
// Main method | |
public static void Main(string[] args) | |
{ | |
Socket clientSocket; | |
byte[] msg = Encoding.ASCII.GetBytes("Hello, Client!\n"); | |
// Set the endpoint options | |
IPEndPoint serv = new IPEndPoint(IPAddress.Any, PortNumber); | |
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
ServerSocket.Bind(serv); | |
// Start listening for connections (queue of 5 max) | |
ServerSocket.Listen(5); | |
// Setup the Ctrl-C | |
Console.CancelKeyPress += InterruptHandler; | |
Running = true; | |
Console.WriteLine("Running the TCP server."); | |
// Main loop | |
while (Running) | |
{ | |
// Wait for a new client (blocks) | |
clientSocket = ServerSocket.Accept(); | |
// Print some infor about the remote client | |
Console.WriteLine("Incoming connection from {0}, replying.", clientSocket.RemoteEndPoint); | |
// Send a reply (blocks) | |
clientSocket.Send(msg, SocketFlags.None); | |
// Close the connection | |
clientSocket.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment