got sending working but only one way?
This commit is contained in:
@@ -86,36 +86,21 @@ void *sender(void *arg) {
|
|||||||
void *receiver(void *arg) {
|
void *receiver(void *arg) {
|
||||||
char buf[128];
|
char buf[128];
|
||||||
int bytes;
|
int bytes;
|
||||||
if (client) {
|
while (1) {
|
||||||
while (1) {
|
pthread_mutex_lock(&receivemut);
|
||||||
pthread_mutex_lock(&receivemut);
|
memset(buf, 0, 128);
|
||||||
memset(buf, 0, 128);
|
bytes = recv(confd, buf, 128, 0);
|
||||||
bytes = recv(newfd, buf, 128, 0);
|
pthread_mutex_unlock(&receivemut);
|
||||||
pthread_mutex_unlock(&receivemut);
|
|
||||||
|
|
||||||
if (bytes != -1) {
|
if (bytes != -1) {
|
||||||
printf("Received: %s\n", buf);
|
printf("Received: %s\n", buf);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
perror("recv");
|
perror("recv");
|
||||||
}
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
while (1) {
|
|
||||||
pthread_mutex_lock(&receivemut);
|
|
||||||
memset(buf, 0, 128);
|
|
||||||
bytes = recv(sockfd, buf, 128, 0);
|
|
||||||
pthread_mutex_unlock(&receivemut);
|
|
||||||
|
|
||||||
if (bytes != -1) {
|
|
||||||
printf("Received: %s\n", buf);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
perror("recv");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,7 +215,7 @@ int main(int argc, char* argv[]) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
for (q = servinfo; q != NULL; q = q->ai_next) {
|
for (q = servinfo; q != NULL; q = q->ai_next) {
|
||||||
if ((newfd = socket(q->ai_family,
|
if ((confd = socket(q->ai_family,
|
||||||
q->ai_socktype,
|
q->ai_socktype,
|
||||||
q->ai_protocol)) == -1) {
|
q->ai_protocol)) == -1) {
|
||||||
perror("Client: socket");
|
perror("Client: socket");
|
||||||
@@ -240,7 +225,7 @@ int main(int argc, char* argv[]) {
|
|||||||
s, sizeof s);
|
s, sizeof s);
|
||||||
printf("Connecting to %s\n", s);
|
printf("Connecting to %s\n", s);
|
||||||
|
|
||||||
if (connect(newfd, q->ai_addr, q->ai_addrlen) == -1) {
|
if (connect(confd, q->ai_addr, q->ai_addrlen) == -1) {
|
||||||
perror("Client: connect");
|
perror("Client: connect");
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
continue;
|
continue;
|
||||||
@@ -252,9 +237,8 @@ int main(int argc, char* argv[]) {
|
|||||||
fprintf(stderr, "Client: failed to create socket\n");
|
fprintf(stderr, "Client: failed to create socket\n");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
else {
|
servinfo = q;
|
||||||
servinfo = q;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pthread_create(&send_thread, NULL, sender, NULL);
|
pthread_create(&send_thread, NULL, sender, NULL);
|
||||||
pthread_create(&receive_thread, NULL, receiver, NULL);
|
pthread_create(&receive_thread, NULL, receiver, NULL);
|
||||||
|
|||||||
Reference in New Issue
Block a user