Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. // Name of Author(s): Joshua Monson
  2. // Course Number and Name: CSE 434, Computer Networks
  3. // Semester: Fall 2016
  4. // Project Part: 2
  5. // Time Spent: ?? hours ?? minutes
  6.  
  7. //input - output declarations included in all C programs
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12.  
  13. //contains definitions of a number of data types used in system calls
  14. #include <sys/types.h>
  15.  
  16. //definitions of structures needed for sockets
  17. #include <sys/socket.h>
  18.  
  19. //in.h contains constants and structures needed for internet domain addresses
  20. #include <netinet/in.h>
  21.  
  22. #define error(msg) perror(msg); exit(1)
  23.  
  24. int main(int argc, char* argv[]) {
  25.  
  26. //variable declarations
  27.  
  28. //sockfd and newsockfd are file descriptors,These two variables store the values returned by the socket system call and the accept system call.
  29. //portno stores the port number on which the server accepts connections.
  30. int port;
  31. int sockfd;
  32. int newsockfd;
  33.  
  34. //clilen stores the size of the address of the client. This is required for the accept system call.
  35. socklen_t clilen;
  36.  
  37. //server reads characters from the socket connection into this buffer.
  38. char buffer[256];
  39.  
  40. //sockaddr_in is a structure containing an internet address
  41. //in_addr structure, contains only one field, a unsigned long called s_addr.
  42. //serv_addr will contain the address of the server, and cli_addr will contain the address of the client which connects to the server.
  43. struct sockaddr_in serv_addr;
  44. struct sockaddr_in cli_addr;
  45. int n;
  46.  
  47. // Check for proper number of arguments
  48. if (argc != 2) {
  49. error("ERROR: Incorrect number of arguments!");
  50. }
  51.  
  52. // Read the port number to start the server on
  53. port = atoi(argv[1]);
  54. printf("Port Number: %d", port);
  55.  
  56. //create socket
  57. //it take three arguments - address domain, type of socket, protocol (zero allows the OS to choose thye appropriate protocols based on type of socket)
  58. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  59. if (sockfd < 0)
  60. error("ERROR opening socket");
  61.  
  62. //set server address buffer with zeros using bzero or memset
  63. //two arguments - pointer to buffer and sizeof buffer
  64. bzero((char *) &serv_addr, sizeof(serv_addr));
  65.  
  66. //contains a code for the address family
  67. serv_addr.sin_family = AF_INET;
  68.  
  69. //contains the IP address of the host
  70. serv_addr.sin_addr.s_addr = INADDR_ANY;
  71.  
  72. //contain the port number
  73. serv_addr.sin_port = htons(port);
  74.  
  75. //bind() system call binds a socket to an address, in this case the address of the current host and port number on which the server will run.
  76. //three arguments, the socket file descriptor, the address to which is bound, and the size of the address to which it is bound.
  77. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  78. error("ERROR on binding");
  79.  
  80. //listen system call allows the process to listen on the socket for connections.
  81. //The first argument is the socket file descriptor, and second is number of connections that can be waiting while the process is handling a particular connection.
  82. listen(sockfd, 5);
  83.  
  84. clilen = sizeof(cli_addr);
  85.  
  86. //accept() system call causes the process to block until a client connects to the server.
  87. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  88.  
  89. if (newsockfd < 0)
  90. error("ERROR on accept");
  91.  
  92. //After a connection a client has successfully connected to the server
  93. //initialize the buffer using the bzero() function
  94. bzero(buffer, 256);
  95.  
  96. //reads from the socket into a buffer for a maximum of 255 characters
  97. //read call uses new file descriptor, the one returned by accept()
  98. n = read(newsockfd, buffer, 255);
  99. if (n < 0)
  100. error("ERROR reading from socket");
  101.  
  102. //both the server can read and write after a connection has been established.
  103. //everything written by the client will be read by the server, and everything written by the server will be read by the client.
  104. printf("Here is the message: %s\n", buffer);
  105. n = write(newsockfd, "I got your message", 18);
  106. if (n < 0)
  107. error("ERROR writing to socket");
  108.  
  109. //close connections using file descriptors
  110. close(newsockfd);
  111. close(sockfd);
  112. printf("Exiting Program\n");
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement