C# Rest Api Upload File and Other Form Data

What is socket programming?
Socket programming is a manner of connecting two nodes on a network to communicate with each other. Ane socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while customer reaches out to the server.

Land diagram for server and customer model

State diagram for server and customer model of Socket

Stages for server

1. Socket creation:

int sockfd = socket(domain, type, protocol)

  • sockfd: socket descriptor, an integer (like a file-handle)
  • domain: integer, specifies communication domain. Nosotros use AF_ LOCAL as divers in the POSIX standard for communication between processes on the same host. For communicating between processes on different hosts connected by IPV4, we use AF_INET and AF_I NET 6 for processes connected by IPV6.
  • type: advice type
    SOCK_STREAM: TCP(reliable, connexion oriented)
    SOCK_DGRAM: UDP(unreliable, connectionless)
  • protocol: Protocol value for Net Protocol(IP), which is 0. This is the same number which appears on protocol field in the IP header of a package.(human being protocols for more than details)

2. Setsockopt: This helps in manipulating options for the socket referred by the file descriptor sockfd. This is completely optional, simply it helps in reuse of address and port. Prevents error such every bit: "accost already in apply".

int setsockopt(int sockfd, int level, int optname,  const void *optval, socklen_t optlen);

three. Bind:

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

Afterwards creation of the socket, bind function binds the socket to the address and port number specified in addr(custom data structure). In the example code, nosotros bind the server to the localhost, hence we use INADDR_ANY to specify the IP accost.

four. Listen:

int mind(int sockfd, int backlog);

It puts the server socket in a passive mode, where it waits for the client to arroyo the server to brand a connection. The backlog, defines the maximum length to which the queue of pending connections for sockfd may grow. If a connexion request arrives when the queue is full, the client may receive an mistake with an indication of ECONNREFUSED.

five. Accept:

int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Information technology extracts the starting time connectedness asking on the queue of pending connections for the listening socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. At this point, connection is established between client and server, and they are prepare to transfer data.

Stages for Client

  • Socket connexion: Exactly same as that of server'due south socket cosmos
  • Connect: The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr. Server's address and port is specified in addr.

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

Implementation
Here we are exchanging one hello message between server and client to demonstrate the client/server model.

  • Server.c

C

#include <netinet/in.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <unistd.h>

#ascertain PORT 8080

int main( int argc, char const * argv[])

{

int server_fd, new_socket, valread;

struct sockaddr_in accost;

int opt = 1;

int addrlen = sizeof (address);

char buffer[1024] = { 0 };

char * hello = "Hello from server" ;

if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))

== 0) {

perror ( "socket failed" );

exit (EXIT_FAILURE);

}

if (setsockopt(server_fd, SOL_SOCKET,

SO_REUSEADDR | SO_REUSEPORT, &opt,

sizeof (opt))) {

perror ( "setsockopt" );

exit (EXIT_FAILURE);

}

address.sin_family = AF_INET;

address.sin_addr.s_addr = INADDR_ANY;

address.sin_port = htons(PORT);

if (bind(server_fd, ( struct sockaddr*)&accost,

sizeof (address))

< 0) {

perror ( "demark failed" );

exit (EXIT_FAILURE);

}

if (listen(server_fd, iii) < 0) {

perror ( "mind" );

leave (EXIT_FAILURE);

}

if ((new_socket

= accept(server_fd, ( struct sockaddr*)&address,

(socklen_t*)&addrlen))

< 0) {

perror ( "accept" );

exit (EXIT_FAILURE);

}

valread = read(new_socket, buffer, 1024);

printf ( "%southward\north" , buffer);

send(new_socket, hello, strlen (hullo), 0);

printf ( "Hello message sent\n" );

return 0;

}

  • customer.c

C

#include <arpa/inet.h>

#include <stdio.h>

#include <string.h>

#include <sys/socket.h>

#include <unistd.h>

#define PORT 8080

int main( int argc, char const * argv[])

{

int sock = 0, valread;

struct sockaddr_in serv_addr;

char * hello = "Hello from client" ;

char buffer[1024] = { 0 };

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {

printf ( "\n Socket creation error \n" );

return -1;

}

serv_addr.sin_family = AF_INET;

serv_addr.sin_port = htons(PORT);

if (inet_pton(AF_INET, "127.0.0.1" , &serv_addr.sin_addr)

<= 0) {

printf (

"\nInvalid address/ Address not supported \n" );

render -i;

}

if (connect(sock, ( struct sockaddr*)&serv_addr,

sizeof (serv_addr))

< 0) {

printf ( "\nConnection Failed \n" );

return -one;

}

ship(sock, hello, strlen (how-do-you-do), 0);

printf ( "Hello message sent\north" );

valread = read(sock, buffer, 1024);

printf ( "%s\due north" , buffer);

return 0;

}

Compiling:

gcc customer.c -o client gcc server.c -o server

Output:

Client:Hello message sent Hello from server Server:Hello from customer Hello message sent

Next: Socket Programming in C/C++: Treatment multiple clients on server without multi threading

This article is contributed by Akshat Sinha. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your commodity to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and aid other Geeks.

Please write comments if you find anything incorrect, or you want to share more information well-nigh the topic discussed above.


pichardopreritch.blogspot.com

Source: https://www.geeksforgeeks.org/socket-programming-cc/

0 Response to "C# Rest Api Upload File and Other Form Data"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel