|
13 | 13 |
|
14 | 14 | #include "chat.h"
|
15 | 15 |
|
16 |
| -int done = FALSE; //boolean that tells threads when client program is finished |
| 16 | +int done = FALSE; //boolean that tells threads when client program is finished |
17 | 17 |
|
18 |
| -int sockfd; //socket file descriptor for client |
| 18 | +int sockfd; //socket file descriptor for client |
19 | 19 |
|
20 |
| -pthread_mutex_t mutexsum = PTHREAD_MUTEX_INITIALIZER; //mutual exclusion for our threads |
| 20 | +pthread_mutex_t mutexsum = PTHREAD_MUTEX_INITIALIZER; //mutual exclusion for our threads |
21 | 21 |
|
22 |
| -void *sender(); // thread function that will take user input and send out messages to server |
| 22 | +void *sender(); // thread function that will take user input and send out messages to server |
23 | 23 |
|
24 |
| -void *receiver(); // thread function that will listen for received messages coming from the server |
| 24 | +void *receiver(); // thread function that will listen for received messages coming from the server |
25 | 25 |
|
26 | 26 | char sendBuffer[BUF_SIZE];
|
27 |
| -char receiveBuffer[BUF_SIZE+USERNAME_LEN+2]; |
28 |
| - |
29 |
| -int main (int argc, char *argv[]){ |
30 |
| - bzero(sendBuffer,BUF_SIZE); //zero out both buffers |
31 |
| - bzero(receiveBuffer,BUF_SIZE+USERNAME_LEN+2); |
32 |
| - |
33 |
| - int portnum; |
34 |
| - char username[USERNAME_LEN]; |
35 |
| - |
36 |
| - if (argc != 4) |
37 |
| - { |
38 |
| - fprintf(stderr, "Usage: %s [server] [portnum] [username]\n", argv[0]); |
39 |
| - exit(EXIT_FAILURE); |
40 |
| - } |
41 |
| - |
42 |
| - portnum = atoi(argv[2]); |
43 |
| - strncpy(username,argv[3],USERNAME_LEN); |
44 |
| - |
45 |
| - printf("server: %s\n",argv[1]); |
46 |
| - printf("port: %d\n",portnum); |
47 |
| - printf("username: %s\n",username); |
48 |
| - |
49 |
| - //allow server to resolve hostnames or use ip's |
50 |
| - struct hostent *server_host; |
51 |
| - if ((server_host=gethostbyname(argv[1])) == NULL) { // get the host info |
52 |
| - fprintf (stderr, "Failed to resolve server host information\n"); |
53 |
| - exit (EXIT_FAILURE); |
54 |
| - } |
55 |
| - |
56 |
| - printf("Host: %s\n", server_host->h_name); |
57 |
| - printf("IP Address of host: %s\n", inet_ntoa((struct in_addr)*((struct in_addr *)server_host->h_addr))); |
58 |
| - |
59 |
| - |
60 |
| - struct sockaddr_in server_addr; // server's internet address used for all sends and receives |
61 |
| - |
62 |
| - sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
63 |
| - if(sockfd == -1){ //socket() returns -1 on error |
64 |
| - close (sockfd); |
65 |
| - fprintf (stderr, "Failed to get socket file descriptor\n"); |
66 |
| - exit (EXIT_FAILURE); |
67 |
| - } |
68 |
| - |
69 |
| - server_addr.sin_family = AF_INET; // host byte order |
70 |
| - server_addr.sin_port = htons(portnum); // short, network byte order |
71 |
| - server_addr.sin_addr = *((struct in_addr *)server_host->h_addr); |
72 |
| - memset(&(server_addr.sin_zero), '\0', 8); // zero the rest of the struct |
73 |
| - |
74 |
| - //Make connection to server socket so we can use send() and recv() to read and write the server |
75 |
| - if(connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == SYSERR){ |
76 |
| - close(sockfd); |
77 |
| - fprintf(stderr, "Failed to connect to remote server!\n"); |
78 |
| - exit(EXIT_FAILURE); |
79 |
| - } |
80 |
| - |
81 |
| - // Create and send out open message to the server so it knows our username and we are identified as a connected client |
82 |
| - |
83 |
| - strcpy(sendBuffer,username); |
84 |
| - if (send(sockfd,sendBuffer,strlen(sendBuffer), 0) == SYSERR){ |
85 |
| - perror("send"); |
86 |
| - close(sockfd); |
87 |
| - exit(EXIT_FAILURE); |
88 |
| - } |
89 |
| - |
90 |
| - |
91 |
| - //create threads |
92 |
| - //Thread 1: takes in user input and sends out messages |
93 |
| - //Thread 2: listens for messages that are comming in from the server and prints them to screen |
94 |
| - // Set up threads |
95 |
| - pthread_t threads[2]; |
96 |
| - pthread_attr_t attr; |
97 |
| - pthread_attr_init(&attr); |
98 |
| - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
99 |
| - |
100 |
| - // Run the sender and receiver threads |
101 |
| - pthread_create(&threads[0], &attr, sender, NULL); |
102 |
| - pthread_create(&threads[1], &attr, receiver, NULL); |
103 |
| - |
104 |
| - // Wait until done is TRUE then exit program |
105 |
| - while(!done); |
106 |
| - |
107 |
| - close(sockfd); |
108 |
| - return OK; |
| 27 | +char receiveBuffer[BUF_SIZE + USERNAME_LEN + 2]; |
| 28 | + |
| 29 | +int |
| 30 | +main(int argc, char *argv[]) { |
| 31 | + bzero(sendBuffer, BUF_SIZE); //zero out both buffers |
| 32 | + bzero(receiveBuffer, BUF_SIZE + USERNAME_LEN + 2); |
| 33 | + |
| 34 | + int portnum; |
| 35 | + char username[USERNAME_LEN]; |
| 36 | + |
| 37 | + if(argc != 4) { |
| 38 | + fprintf(stderr, "Usage: %s [server] [portnum] [username]\n", argv[0]); |
| 39 | + exit(EXIT_FAILURE); |
| 40 | + } |
| 41 | + |
| 42 | + portnum = atoi(argv[2]); |
| 43 | + strncpy(username, argv[3], USERNAME_LEN); |
| 44 | + |
| 45 | + printf("server: %s\n", argv[1]); |
| 46 | + printf("port: %d\n", portnum); |
| 47 | + printf("username: %s\n", username); |
| 48 | + |
| 49 | + //allow server to resolve hostnames or use ip's |
| 50 | + struct hostent *server_host; |
| 51 | + |
| 52 | + if((server_host = gethostbyname(argv[1])) == NULL) { // get the host info |
| 53 | + fprintf(stderr, "Failed to resolve server host information\n"); |
| 54 | + exit(EXIT_FAILURE); |
| 55 | + } |
| 56 | + |
| 57 | + printf("Host: %s\n", server_host->h_name); |
| 58 | + printf("IP Address of host: %s\n", inet_ntoa((struct in_addr) |
| 59 | + *((struct in_addr *) |
| 60 | + server_host->h_addr))); |
| 61 | + |
| 62 | + struct sockaddr_in server_addr; // server's internet address used for all sends and receives |
| 63 | + |
| 64 | + sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 65 | + if(sockfd == -1) { //socket() returns -1 on error |
| 66 | + close(sockfd); |
| 67 | + fprintf(stderr, "Failed to get socket file descriptor\n"); |
| 68 | + exit(EXIT_FAILURE); |
| 69 | + } |
| 70 | + |
| 71 | + server_addr.sin_family = AF_INET; // host byte order |
| 72 | + server_addr.sin_port = htons(portnum); // short, network byte order |
| 73 | + server_addr.sin_addr = *((struct in_addr *) server_host->h_addr); |
| 74 | + memset(&(server_addr.sin_zero), '\0', 8); // zero the rest of the struct |
| 75 | + |
| 76 | + //Make connection to server socket so we can use send() and recv() to read and write the server |
| 77 | + if(connect(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) == SYSERR) { |
| 78 | + close(sockfd); |
| 79 | + fprintf(stderr, "Failed to connect to remote server!\n"); |
| 80 | + exit(EXIT_FAILURE); |
| 81 | + } |
| 82 | + // Create and send out open message to the server so it knows our username and we are identified as a connected client |
| 83 | + |
| 84 | + strcpy(sendBuffer, username); |
| 85 | + if(send(sockfd, sendBuffer, strlen(sendBuffer), 0) == SYSERR) { |
| 86 | + perror("send"); |
| 87 | + close(sockfd); |
| 88 | + exit(EXIT_FAILURE); |
| 89 | + } |
| 90 | + //create threads |
| 91 | + //Thread 1: takes in user input and sends out messages |
| 92 | + //Thread 2: listens for messages that are comming in from the server and prints them to screen |
| 93 | + // Set up threads |
| 94 | + pthread_t threads[2]; |
| 95 | + pthread_attr_t attr; |
| 96 | + |
| 97 | + pthread_attr_init(&attr); |
| 98 | + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 99 | + |
| 100 | + // Run the sender and receiver threads |
| 101 | + pthread_create(&threads[0], &attr, sender, NULL); |
| 102 | + pthread_create(&threads[1], &attr, receiver, NULL); |
| 103 | + |
| 104 | + // Wait until done is TRUE then exit program |
| 105 | + while(!done); |
| 106 | + |
| 107 | + close(sockfd); |
| 108 | + return OK; |
109 | 109 | }
|
110 | 110 |
|
| 111 | +void * |
| 112 | +sender() { |
111 | 113 |
|
112 |
| -void *sender(){ |
| 114 | + while(1) { |
| 115 | + bzero(sendBuffer, BUF_SIZE); |
113 | 116 |
|
114 |
| - while(1){ |
115 |
| - bzero(sendBuffer,BUF_SIZE); |
| 117 | + fgets(sendBuffer, BUF_SIZE, stdin); |
116 | 118 |
|
117 |
| - fgets(sendBuffer, BUF_SIZE, stdin); |
| 119 | + //send message to server |
| 120 | + if(send(sockfd, sendBuffer, strlen(sendBuffer), 0) == SYSERR) { |
| 121 | + perror("send"); |
| 122 | + done = TRUE; |
| 123 | + pthread_mutex_destroy(&mutexsum); |
| 124 | + pthread_exit(NULL); |
| 125 | + } |
| 126 | + // Check for quiting |
| 127 | + if(strcmp(sendBuffer, CLOSE) == 0 || strcmp(sendBuffer, EXIT) == 0) { |
118 | 128 |
|
119 |
| - //send message to server |
120 |
| - if (send(sockfd,sendBuffer,strlen(sendBuffer), 0) == SYSERR){ |
121 |
| - perror("send"); |
122 |
| - done = TRUE; |
123 |
| - pthread_mutex_destroy(&mutexsum); |
124 |
| - pthread_exit(NULL); |
125 |
| - } |
126 |
| - // Check for quiting |
127 |
| - if(strcmp(sendBuffer,CLOSE)==0 || strcmp(sendBuffer,EXIT) == 0){ |
| 129 | + done = TRUE; |
| 130 | + pthread_mutex_destroy(&mutexsum); |
| 131 | + pthread_exit(NULL); |
128 | 132 |
|
129 |
| - done = TRUE; |
130 |
| - pthread_mutex_destroy(&mutexsum); |
131 |
| - pthread_exit(NULL); |
132 |
| - |
133 |
| - } |
134 |
| - pthread_mutex_unlock (&mutexsum); |
135 |
| - } |
| 133 | + } |
| 134 | + pthread_mutex_unlock(&mutexsum); |
| 135 | + } |
136 | 136 | }
|
137 | 137 |
|
138 |
| - |
139 |
| -void *receiver(){ |
140 |
| - int nbytes; |
141 |
| - while(1){ |
142 |
| - bzero(receiveBuffer,BUF_SIZE); |
143 |
| - |
144 |
| - //Receive messages from server |
145 |
| - if ((nbytes = recv(sockfd, receiveBuffer, BUF_SIZE-1, 0)) == SYSERR) { |
146 |
| - perror("recv"); |
147 |
| - done = TRUE; |
148 |
| - pthread_mutex_destroy(&mutexsum); |
149 |
| - pthread_exit(NULL); |
150 |
| - } |
151 |
| - receiveBuffer[nbytes] = '\0'; |
152 |
| - printf("%s",receiveBuffer); |
153 |
| - pthread_mutex_unlock (&mutexsum); |
154 |
| - } |
| 138 | +void * |
| 139 | +receiver() { |
| 140 | + int nbytes; |
| 141 | + |
| 142 | + while(1) { |
| 143 | + bzero(receiveBuffer, BUF_SIZE); |
| 144 | + |
| 145 | + //Receive messages from server |
| 146 | + if((nbytes = recv(sockfd, receiveBuffer, BUF_SIZE - 1, 0)) == SYSERR) { |
| 147 | + perror("recv"); |
| 148 | + done = TRUE; |
| 149 | + pthread_mutex_destroy(&mutexsum); |
| 150 | + pthread_exit(NULL); |
| 151 | + } |
| 152 | + receiveBuffer[nbytes] = '\0'; |
| 153 | + printf("%s", receiveBuffer); |
| 154 | + pthread_mutex_unlock(&mutexsum); |
| 155 | + } |
155 | 156 | }
|
156 |
| - |
157 |
| - |
158 |
| - |
0 commit comments