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: Version: Dev-C beta 9.2 ( ) Download and install the distributions – _setup.exe _setup.exe Configuration – [Tools]\[Compiler Options]: » Add these commands to the linker command line: -lwsock32
– Java: Java Standard Edition Development Kit (JDK) (for Linux/Windows and other environments) Homepage: nloads/index.html nloads/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
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
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
Sending/Receiving Packets – an Example ECHO client/server socket() close() sendto() recvfrom() socket() close() recvfrom() sendto() bind() An ECHO clientAn ECHO server
ECHO client (1/3) #include #define MAX_BUF char buf[MAX_BUF+1]; char Request[MAX_BUF+1];
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, " "); // 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);
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; }
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()! "); }
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); }
HTTP Request
HTTP Response