Assignment №1
1 Simple Messanger
You need to develop the simplest instant messaging system (chat, messenger)
1.1 Server
Must receive messages from clients and send messages to all connected clients.
Use threads to work with multiple clients. The name of the binary file being
executed must be server. The server must have a console interface in the
parameters to which is transferred: port
1.2 Client
Each client must have own nickname, set by the user. When you receive a
message from another client, the screen should display the time of receiving the
message, the user-sender’s nickname, and the text of the message. An example:
{05:20} [John] Hi!
The client must have a console interface. The name of the client for binary
file. The client accepts the work options through the command line arguments
in the following order: server address, port, nickname.
To prevent the received (incoming) messages from interfering with the user’s
typing, it is suggested that a separate mode of sending a message, for example,
when the m key is pressed, the user enters his message, new messages from
other users are temporarily not displayed, after sending the message (by Enter)
the mode is automatically turned off.
1.3 Protocol
Client -> Server
Nickname size Nick Body size Body
4 bytes Nickname size bytes 4 bytes Body size bytes
Network format - Network format -
Server -> Client
Nickname size Nick Body size Body Date size Date
4 bytes
Nickname size
bytes
4 bytes Body size bytes 4 bytes
Data size
bytes
Network format - Network format - Network format -
11.4 Features that are not required of the server and the
client
• Client registration, authorization, authentication
• More than one channel of communication
• Keeping your correspondence history
• Processing time zones
• Working with languages other than English
1.5 Requirements
• The server and the client must be written in the language of the C
• Make should be used as an build system
• The code must be successfully compiled and worked for Linux and MacOS
• Valgrind and Google Thread Sanitizer should not find errors in the code
server.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <netdb.h>
5 #include <netinet/in.h>
6 #include <unistd.h>
7
8 #include <string.h>
9
10 int main(int argc, char *argv[]) {
11 int sockfd, newsockfd;
12 uint16_t portno;
13 unsigned int clilen;
14 char buffer[256];
15 struct sockaddr_in serv_addr, cli_addr;
16 ssize_t n;
17
18 /* First call to socket() function */
19 sockfd = socket(AF_INET, SOCK_STREAM, 0);
20
21 if (sockfd < 0) {
22 perror("ERROR opening socket");
23 exit(1);
24 }
25
226 /* Initialize socket structure */
27 bzero((char *) &serv_addr, sizeof(serv_addr));
28 portno = 5001;
29
30 serv_addr.sin_family = AF_INET;
31 serv_addr.sin_addr.s_addr = INADDR_ANY;
32 serv_addr.sin_port = htons(portno);
33
34 /* Now bind the host address using bind() call.*/
35 if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
36 perror("ERROR on binding");
37 exit(1);
38 }
39
40 /* Now start listening for the clients, here process will
41 * go in sleep mode and will wait for the incoming connection
42 */
43
44 listen(sockfd, 5);
45 clilen = sizeof(cli_addr);
46
47 /* Accept actual connection from the client */
48 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
49
50 if (newsockfd < 0) {
51 perror("ERROR on accept");
52 exit(1);
53 }
54
55 /* If connection is established then start communicating */
56 bzero(buffer, 256);
57 n = read(newsockfd, buffer, 255);
58
59 if (n < 0) {
60 perror("ERROR reading from socket");
61 exit(1);
62 }
63
64 printf("Here is the message: %s\n", buffer);
65
66 /* Write a response to the client */
67 n = write(newsockfd, "I got your message", 18);
68
69 if (n < 0) {
70 perror("ERROR writing to socket");
71 exit(1);
372 }
73
74 return 0;
75 }
client.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <netdb.h>
5 #include <netinet/in.h>
6 #include <unistd.h>
7
8 #include <string.h>
9
10 int main(int argc, char *argv[]) {
11 int sockfd, n;
12 uint16_t portno;
13 struct sockaddr_in serv_addr;
14 struct hostent *server;
15
16 char buffer[256];
17
18 if (argc < 3) {
19 fprintf(stderr, "usage %s hostname port\n", argv[0]);
20 exit(0);
21 }
22
23 portno = (uint16_t) atoi(argv[2]);
24
25 /* Create a socket point */
26 sockfd = socket(AF_INET, SOCK_STREAM, 0);
27
28 if (sockfd < 0) {
29 perror("ERROR opening socket");
30 exit(1);
31 }
32
33 server = gethostbyname(argv[1]);
34
35 if (server == NULL) {
36 fprintf(stderr, "ERROR, no such host\n");
37 exit(0);
38 }
39
40 bzero((char *) &serv_addr, sizeof(serv_addr));
441 serv_addr.sin_family = AF_INET;
42 bcopy(server->h_addr, (char *) &serv_addr.sin_addr.s_addr, (size_t) server->h_length);
43 serv_addr.sin_port = htons(portno);
44
45 /* Now connect to the server */
46 if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
47 perror("ERROR connecting");
48 exit(1);
49 }
50
51 /* Now ask for a message from the user, this message
52 * will be read by server
53 */
54
55 printf("Please enter the message: ");
56 bzero(buffer, 256);
57 fgets(buffer, 255, stdin);
58
59 /* Send message to the server */
60 n = write(sockfd, buffer, strlen(buffer));
61
62 if (n < 0) {
63 perror("ERROR writing to socket");
64 exit(1);
65 }
66
67 /* Now read server response */
68 bzero(buffer, 256);
69 n = read(sockfd, buffer, 255);
70
71 if (n < 0) {
72 perror("ERROR reading from socket");
73 exit(1);
74 }
75
76 printf("%s\n", buffer);
77 return 0;
78 }
5
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。