13
Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

Embed Size (px)

Citation preview

Page 1: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

Lab #1: Network Programming using Sockets

By J. H. WangNov. 28, 2011

Page 2: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

Platform and Compiler

– Gcc: on UNIX/Linux– Dev-C++: on Windows• Homepage: http://www.bloodshed.net/devcpp.html• Version: Dev-C++ 5.0 beta 9.2 (4.9.9.2)• Download and install the distributions

– http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe

• Configuration– [Tools]\[Compiler Options]:

» Add these commands to the linker command line: -lwsock32

Page 3: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

– Java: Java Standard Edition Development Kit (JDK) (for Linux/Windows and other environments)• Homepage:

http://www.oracle.com/technetwork/java/javase/downloads/index.html• Version: JDK 7u1 or 6u29• Download and install the JDK• Configure Java environment (on Linux, for example)

– setenv JAVA_HOME /usr/lib64/java– setenv MANPATH ${MANPATH}:${JAVA_HOME}/man– setenv PATH ${PATH}:${JAVA_HOME}/bin

Page 4: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

Network Programming Exercises

• Sending/receiving a packet– E.g. Echo client/server (port 7)

• Connecting to the server: – E.g. Web server (HTTP requests)

• Parsing the response from the server:– E.g. HTTP responses

Page 5: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

• Datagram sockets – connectionless (UDP)– No need to establish connections before

sending/receiving packets

• Stream socket – connection-oriented (TCP)– Establish a connection– Sending/receiving packets– Closing the connection

Page 6: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

Sending/Receiving Packets – an Example ECHO client/server

socket()socket()

close()close()

sendto()sendto()

recvfrom()recvfrom()

socket()socket()

close()close()

recvfrom()recvfrom()

sendto()sendto()

bind()bind()

An ECHO client An ECHO server

Page 7: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

ECHO client (1/3)• #include <stdio.h>• #include <stdlib.h>• #include <errno.h>• #include <sys/types.h>• #include <sys/socket.h>• #include <netinet/in.h>

• #define MAX_BUF 163840• char buf[MAX_BUF+1];• char Request[MAX_BUF+1];

Page 8: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

ECHO client (2/3)• int main(int argc, char **argv)• {• int nSocket, nLen, n;• char ip[128];• struct sockaddr_in in;• struct sockaddr_in out;• if (argc > 1)• strcpy(ip, argv[1]);• else• strcpy(ip, "127.0.0.1"); // default: send to localhost.• memset(Request, 0, MAX_BUF);• printf("Please enter the message to the server (%s): \n", ip);• scanf("%s", Request);• printf("Sending the message to host: %s...\n", ip);

Page 9: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

ECHO client (3/3)• nSocket = socket(AF_INET, SOCK_DGRAM, 0);• memset(&out, 0, sizeof(out));• out.sin_family = AF_INET;• out.sin_port = htons(0x07); // port 7: Echo Service• out.sin_addr.s_addr = inet_addr(ip);• n = sendto(nSocket, Request, strlen(Request), 0, (struct sockaddr*)&out,

sizeof(out));• printf("%d bytes sent to %08lx...\n", n, ntohl(out.sin_addr.s_addr));• n = recvfrom(nSocket, buf, MAX_BUF, 0, (struct sockaddr*)&in, &nLen);• if (n == -1)• perror("[ECHOc] ERROR!\n");• else• printf("%d bytes received: \"%s\"\n", n, buf);• close(nSocket);• return 0;• }

Page 10: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

ECHO server (1/2)• nSocket = socket(AF_INET, SOCK_DGRAM, 0);• memset(&server, 0, sizeof(server));• server.sin_family = AF_INET;• server.sin_port = htons(0x07); // ECHO server• server.sin_addr.s_addr = INADDR_ANY;• ret = bind(nSocket, (struct sockaddr*)&server,

sizeof(server));• if (ret==-1)• {• perror("[ECHOd] Error in bind()! ");• }

Page 11: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

ECHO server (2/2)• while (1)• {• printf("[ECHOd] Waiting for requests...\n");• nLen = sizeof(client);• n = recvfrom(nSocket, buf, MAX_BUF, 0, (struct sockaddr

*)&client, &nLen);• buf[n] = '\0';• printf("%d bytes received from %08lx...\n", n,

ntohl(client.sin_addr.s_addr));• printf("==> [ECHOd] \"%s\"\n", buf);• n = sendto(nSocket, buf, strlen(buf), 0, (struct sockaddr *)&client,

nLen);• printf("%d bytes sent...\n", n);• printf("[ECHOd] ==> \"%s\"\n", buf);• }

Page 12: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

HTTP Request

Page 13: Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011

HTTP Response