//====================================================== file = server1.c ==== //= A message "server" program to demonstrate sockets programming = //= - UDP/IP client/server model is implemented = //============================================================================ //= Notes: = //= 1) This program conditionally compiles for Winsock and BSD sockets. = //= Set the initial #define to WIN or BSD as appropriate. = //= 2) There is *no* error checking in this program. Error checking was = //= omitted to simplify the program. = //= 3) This program serves a message to program client1 running on = //= another host. = //= 4) This program assumes that the client1 has the IP address hardcoded = //= into "#define IP_ADDR" = //= 5) The steps #'s correspond to lecture topics. = //=--------------------------------------------------------------------------= //= Build: bcc32 UDPServer.c or cl server1.c wsock32.lib for Winsock = //= gcc server1.c -lsocket -lnsl for BSD = //=--------------------------------------------------------------------------= //= History: KJC (03/02/99) - Genesis (from server.c) = //============================================================================ #define WIN // WIN for Winsock and BSD for BSD sockets //----- Include files ------------------------------------------------------- #include // Needed for printf() #include // Needed for memcpy() and strcpy() #ifdef WIN #include // Needed for all Winsock stuff #endif #ifdef BSD #include // Needed for system defined identifiers. #include // Needed for internet address structure. #include // Needed for socket(), bind(), etc... #include // Needed for inet_ntoa() #include // Needed for sockets stuff #include // Needed for sockets stuff #endif //----- Defines ------------------------------------------------------------- #define PORT_NUM 1050 // Port number used #define IP_ADDR "127.0.0.1" // IP address of server1 (*** HARDWIRED ***) //===== Main program ======================================================== void main(void) { #ifdef WIN WORD wVersionRequested = MAKEWORD(1,1); // Stuff for WSA functions WSADATA wsaData; // Stuff for WSA functions #endif unsigned int server_s; // Server socket descriptor struct sockaddr_in server_addr; // Server1 Internet address struct sockaddr_in client_addr; // Client1 Internet address int addr_len; // Internet address length char out_buf[100]; // 100-byte buffer for output data char in_buf[100]; // 100-byte buffer for input data long int i; // Loop counter #ifdef WIN // This stuff initializes winsock WSAStartup(wVersionRequested, &wsaData); #endif // >>> Step #1 <<< // Create a socket // - AF_INET is Address Family Internet and SOCK_DGRAM is datagram server_s = socket(AF_INET, SOCK_DGRAM, 0); // >>> Step #2 <<< // Fill-in my socket's address information server_addr.sin_family = AF_INET; // Address family to use server_addr.sin_port = htons(PORT_NUM); // Port number to use server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on any IP address bind(server_s, (struct sockaddr *)&server_addr, sizeof(server_addr)); // >>> Step #3 <<< // Fill-in client1 socket's address information client_addr.sin_family = AF_INET; // Address family to use client_addr.sin_port = htons(PORT_NUM); // Port num to use client_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use // >>> Step #4 <<< // Wait to receive a message from client1 addr_len = sizeof(client_addr); recvfrom(server_s, in_buf, sizeof(in_buf), 0, (struct sockaddr *)&client_addr, &addr_len); // Output the received message printf("Message received is: '%s' \n", in_buf); // Assign a message to buffer out_buf strcpy(out_buf, "Reply message from server1 to client1"); // >>> Step #5 <<< // Now send the message to client1. The "+ 1" is for the end-of-string // delimiter sendto(server_s, out_buf, (strlen(out_buf) + 1), 0, (struct sockaddr *)&client_addr, sizeof(client_addr)); // >>> Step #6 <<< // Close all open sockets #ifdef WIN closesocket(server_s); #endif #ifdef BSD close(server_s); #endif #ifdef WIN // This stuff cleans-up winsock WSACleanup(); #endif }