Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011."— Presentation transcript:

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

2 Platform and Compiler – Gcc: on UNIX/Linux – Dev-C++: on Windows Homepage: http://www.bloodshed.net/devcpp.htmlhttp://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 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

3 – Java: Java Standard Edition Development Kit (JDK) (for Linux/Windows and other environments) Homepage: http://www.oracle.com/technetwork/java/javase/dow nloads/index.html http://www.oracle.com/technetwork/java/javase/dow 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

4 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

5 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

6 Sending/Receiving Packets – an Example ECHO client/server socket() close() sendto() recvfrom() socket() close() recvfrom() sendto() bind() An ECHO clientAn ECHO server

7 ECHO client (1/3) #include #define MAX_BUF 163840 char buf[MAX_BUF+1]; char Request[MAX_BUF+1];

8 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);

9 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; }

10 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()! "); }

11 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); }

12 HTTP Request

13 HTTP Response


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

Similar presentations


Ads by Google