Writing a TCP Client-Server Application: C Code and Explanation, Slides of Network Programming

The code and explanation for creating a simple tcp client-server application using winsock in c. The client connects to the server, receives a string, and exits, while the server waits for new connections and sends back a greeting.

Typology: Slides

2011/2012

Uploaded on 07/06/2012

umi
umi 🇮🇳

4.5

(13)

63 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Network Programming Primer
Day 04
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Writing a TCP Client-Server Application: C Code and Explanation and more Slides Network Programming in PDF only on Docsity!

Network Programming Primer

Day 04

Writing a client server

Application

• Server waits for the client to connect

• Client connects

• Server sends back a string

• Client prints the string and exits

• Server again starts waiting for new connection

TCP based client contd.

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

int sockfd, numbytes;

char buf[MAXDATASIZE];

struct sockaddr_in their_addr;

// connector’s address information

TCP based client contd.

WSADATA wsaData;

WSADATA

The WSADATA structure contains information about

the Windows Sockets implementation.

TCP based client contd.

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { fprintf(stderr, "WSAStartup failed.\n"); exit(1); }

TCP based client contd.

int WSAStartup( WORD wVersionRequested, LPWSADATA lpWSAData ); The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an applicati on or DLL to specify the version of Windows Sockets required and retrieve details of the specific Windows Sockets implemen tation. The application or DLL can only issue further Windows Sockets functions after successfully calling WSAStartup.

TCP based client contd.

if (argc != 2) { fprintf(stderr,"usage: client serverIP\n"); exit(1); }

TCP based client contd.

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); WSACleanup( ); exit(1); }

TCP based client contd.

their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(PORT); // network byte order their_addr.sin_addr.s_addr = inet_addr(argv[1]); memset(&(their_addr.sin_zero), 0, 8); // zero rest of the struct

TCP based client contd.

if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr_in)) == -1) { perror("connect"); WSACleanup( ); exit(1); }

TCP based client contd.

buf[numbytes] = 0; printf("Received: %s",buf); closesocket(sockfd); WSACleanup( ); return 0; } //main ends here

TCP based server

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <winsock.h>

#define MYPORT 13490

// the port users will be connecting to

#define BACKLOG 10

// how many pending connections queue will hold docsity.com

TCP based server contd

WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { fprintf(stderr, "WSAStartup failed.\n"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); WSACleanup( ); exit(1); }

TCP based server contd

my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(MYPORT); // network byte order my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP memset(&(my_addr.sin_zero), 0, 8); // zero rest of the struct