1 / 14

CS 471/571

CS 471/571. Sockets 3. Echo UDP Client in C. //includes not shown int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in server; unsigned int clen; int len; char msg[100]; sockfd = socket(AF_INET, SOCK_DGRAM, 0); memset(&server, 0, sizeof(struct sockaddr_in));

fausto
Download Presentation

CS 471/571

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 471/571 Sockets 3

  2. Echo UDP Client in C //includes not shown int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in server; unsigned int clen; int len; char msg[100]; sockfd = socket(AF_INET, SOCK_DGRAM, 0); memset(&server, 0, sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_port = htons(12000); }

  3. Echo UDP Client in C clen = sizeof(server); len = strlen(argv[1]); sendto(sockfd, argv[1], len, 0, (struct sockaddr *) &server, clen); len = recvfrom(sockfd, msg, 100, 0, NULL, NULL); msg[len] = '\0'; printf("%s\n", msg); close(sockfd); }

  4. Echo UDP Server in C //includes not shown int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in server; struct sockaddr_in client; char msg[100]; unsigned int clen; int len; int i; sockfd = socket(AF_INET, SOCK_DGRAM, 0); memset(&server, 0, sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_port = htons(12000);

  5. Echo UDP Server in C bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)); clen = sizeof(client); len = recvfrom(sockfd, msg, 100, 0, (struct sockaddr *) &client, &clen); for (i = 0; i < len; i++) msg[i] = toupper(msg[i]); sendto(sockfd, msg, len, 0, (struct sockaddr *) &client, clen); close(sockfd); }

  6. C Version of TCP Echo Client #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <arpa/inet.h> #include <unistd.h>

  7. C Version of TCP Echo Client int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in server; char msg[100]; sockfd = socket(AF_INET, SOCK_STREAM, 0); memset(&server, 0, sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_port = htons(12000); connect(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)); send(sockfd, argv[1], strlen(argv[1]),0); int len = recv(sockfd, msg, 100, 0); msg[len] = '\0'; printf("%s\n", msg); close(sockfd); }

  8. C Version of TCP Echo Server #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <arpa/inet.h> #include <unistd.h> #include <ctype.h>

  9. C Version of TCP Echo Server int main(int argc, char *argv[]) { int sockfd1, sockfd2; struct sockaddr_in server; struct sockaddr_in client; char msg[100]; unsigned int clen; int len; int i; sockfd1 = socket(AF_INET, SOCK_STREAM, 0); memset(&server, 0, sizeof(struct sockaddr_in)); server.sin_family = AF_INET; server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_port = htons(12000); bind(sockfd1, (struct sockaddr *) &server, sizeof(struct sockaddr_in));

  10. C Version of TCP Echo Server listen(sockfd1, 5); sockfd2 = accept(sockfd1, (struct sockaddr *) &client, &clen); len = recv(sockfd2, msg, 100, 0); for (i = 0; i < len; i++) msg[i] = toupper(msg[i]); send(sockfd2, msg, len, 0); close(sockfd2); close(sockfd1); }

  11. Java Line Number Client (TCP) import java.io.*; import java.net.*; public class LineNumClient { public static void main(String[] args) throws Exception { Socket mySock = new Socket("localhost", 12000); BufferedReader inSock = new BufferedReader( new InputStreamReader(mySock.getInputStream())); PrintStream outSock = new PrintStream( mySock.getOutputStream()); BufferedReader inFile = new BufferedReader(new FileReader(args[0])); String line = inFile.readLine(); while (line != null) { outSock.println(line); System.out.println(inSock.readLine()); line = inFile.readLine(); } mySock.close(); } }

  12. Java Line Number Server (TCP) with Threads import java.io.*; import java.net.*; public class LineNumServer implements Runnable { Socket client; public LineNumServer(Socket s) { client = s; }

  13. Java Line Number Server (TCP) with Threads public void run() { int num = 0; try { BufferedReader inSock = new BufferedReader( new InputStreamReader(client.getInputStream())); PrintStream outSock = new PrintStream( client.getOutputStream()); String line = inSock.readLine(); while (line != null) { num++; outSock.println( "("+num+","+line.length()+")\t"+line); line = inSock.readLine(); } client.close(); } catch (Exception e) { } }

  14. Java Line Number Server (TCP) with Threads public static void main(String[] args) throws Exception { ServerSocket mySock = new ServerSocket(12000); while (true) { Socket client = mySock.accept(); LineNumServer s = new LineNumServer(client); Thread t = new Thread(s); t.start(); } } }

More Related