Docsity

A Stream Socket Client Demo II-Network Programming-Lab Report, Exercises of Network Programming

Toggle sidebar
bg1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
# pragma comment (lib,"wsock32.lib")
#define PORT 13490
// the port client will be connecting to
#define MAXDATASIZE 100
// max number of bytes we can get at once
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct sockaddr_in their_addr;
// connector’s address information
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
exit(0);
}
if (argc != 2)
{
fprintf(stderr,"usage: client serverIP\n");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
WSACleanup( );
exit(1);
}
docsity.com
pf2

Partial preview of the text

Download A Stream Socket Client Demo II-Network Programming-Lab Report and more Exercises Network Programming in PDF only on Docsity!

#include #include #include #include

pragma comment (lib,"wsock32.lib")

#define PORT 13490 // the port client will be connecting to #define MAXDATASIZE 100 // max number of bytes we can get at once int main(int argc, char argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct sockaddr_in their_addr; // connector’s address information WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { fprintf(stderr, "WSAStartup failed.\n"); exit(1); } if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) { / Tell the user that we could not find a usable / / WinSock DLL. */ WSACleanup( ); exit(0); } if (argc != 2) { fprintf(stderr,"usage: client serverIP\n"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); WSACleanup( ); exit(1); }

docsity.com

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 if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr_in)) == -1) { perror("connect"); WSACleanup( ); exit(1); } if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); WSACleanup( ); exit(1); } buf[numbytes] = 0; printf("Received: %s",buf); closesocket(sockfd); WSACleanup( ); return 0; }

docsity.com