Question: Now my vcsend code is getting errors here are the errors below error: expected ' ( ' at end of input 81 | if |
Now my vcsend code is getting errors here are the errors below
error: expected '(' at end of input
81 | if
|
|(
vcsend.c:81:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
81 | if
| ^~
vcsend.c:81: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
81 | if
|
vcsend.c:81:5: error: expected declaration or statement at end of input
81 | if
| ^~
vcsend.c:81:5: error: expected declaration or statement at end of input
vcsend.c:16:15: warning: unused variable 'length' [-Wunused-variable]
16 | int i, ret, length;
| ^~~~~~
vcsend.c:16:7: warning: unused variable 'i' [-Wunused-variable]
16 | int i, ret, length;
| ^
make: *** [Makefile:7: vcsend] Error 1
here is the modified version of vcsend
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[], char *envp[]) {
int msgsock; /* The data socket for the client */
struct sockaddr_in name;
struct hostent *hp; /* Used to resolve hostname to IP address */
char chrline[80];
int i, ret, length;
fd_set readfds;
int max_sd;
struct timeval timeout;
/* We must give the hostname and portnumber on the command line. This
* checks that the user has typed both. */
if (argc < 3) {
printf("error: Invoke this program via: ");
printf(" vcsend
return (-1);
}
/* Since we are allowing a domain name as the hostname, we must call
* gethostbyname() to convert the hostname into an IP address. */
hp = gethostbyname(argv[1]);
if (hp == NULL) {
printf("sender: gethostbyname() failed. ");
printf(" Are you sure the hostname is correct? ");
return (-1);
}
printf(" sender: hostname has been resolved. ");
/* Copy the resolved IP address over into the name structure along with
* the portnumber given on the command line */
memcpy((char *)&name.sin_addr, (char *)hp->h_addr, hp->h_length);
name.sin_family = AF_INET;
name.sin_port = htons((short)atoi(argv[2]));
/* Allocate the data socket and attempt to connect to vcrec. Exit the
* program if either socket() or connect() fails. */
printf("sender: Attempting to connect to server. ");
msgsock = socket(AF_INET, SOCK_STREAM, 0);
if (msgsock < 0) {
perror("sender: socket() failed. ");
return (-1);
}
ret = connect(msgsock, (struct sockaddr *)&name, sizeof name);
if (ret != 0) {
perror("sender: connect() failed. ");
fprintf(stderr,
" are you sure that the portnumber is correct? ");
fflush(stderr);
return (-1);
}
/* Wait for the handshake from vcrec before continuing */
printf("sender: Waiting for handshake from vcrec... ");
sleep(1); /* pause for clean screen display */
fflush(stdout);
recv(msgsock, chrline, sizeof(chrline) - 1, 0); /* Handshake character */
printf("sender: Handshake received. Entering data loop. ");
printf(" Enter a line of characters at the prompt. ");
printf("To exit the program, type just a period and hit enter. ");
while(1) {
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
FD_SET(msgsock, &readfds);
max_sd = msgsock > STDIN_FILENO ? msgsock : STDIN_FILENO;
timeout.tv_sec = 10; // Wait for 10 seconds
timeout.tv_usec = 0;
ret = select(max_sd + 1, &readfds, NULL, NULL, &timeout);
if
here is modified vcrec
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_BUF_SIZE 1024
int main(int argc, char *argv[], char *envp[]) {
int sock, msgsock;
struct sockaddr_in name, caller;
int size, length, ret, k;
char buf[MAX_BUF_SIZE];
if (argc > 1) {
size = atoi(argv[1]);
if (size < 1 || size > sizeof(buf) - 1) {
size = sizeof(buf) - 1;
}
} else {
size = sizeof(buf) - 1;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("receiver: socket() failed. ");
return (-1);
}
memset(&name, 0, sizeof name);
name.sin_family = AF_INET;
name.sin_addr.s_addr = INADDR_ANY;
name.sin_port = htons(0);
ret = bind(sock, (struct sockaddr *)&name, sizeof name);
if (ret < 0) {
perror("receiver: bind() failed. ");
return (-1);
}
length = sizeof name;
ret = getsockname(sock, (struct sockaddr *)&name, (socklen_t *)&length);
if (ret < 0) {
perror("receiver: getsockname() failed. ");
return (-1);
}
sleep(1);
printf(" receiver: process id: %d ", (int)getpid());
printf(" receiver: IP address: %s", inet_ntoa(name.sin_addr));
printf(" receiver: port number: %d ", ntohs(name.sin_port));
listen(sock, 5);
length = sizeof caller;
msgsock = accept(sock, (struct sockaddr *)&caller, (socklen_t *)&length);
if (msgsock < 0) {
perror("receiver: accept() failed. ");
return (-1);
}
do {
ret = recv(msgsock, buf, size, 0);
if (ret < 0) {
perror("receiver: recv() failed. ");
return (-1);
} else if (ret == 0) {
printf(" receiver: Connection closed. ");
break;
}
buf[ret] = '\0';
printf(" Received message: %s", buf);
printf(" Enter message to send: ");
fgets(buf, sizeof buf, stdin);
if (buf[0] == '.') {
break;
}
ret = send(msgsock, buf, strlen(buf), 0);
if (ret < 0) {
perror("receiver: send() failed. ");
return (-1);
}
} while (1);
close(msgsock);
close(sock);
printf(" receiver: Done. ");
return 0;
}
Also can you show me both the modified versions of both files and not just send or receive but both, thank you
IN
Do select() to decide if you are going to call send() or recv(). when setup for select(), tell it to monitor both stdin and the data socket for read activities
Modify the code to allow full-duplex (simple two-way) communication by having the following interaction between the two programs: vcsend asks the user for a string and sends it to vcrec vcrec echos the received string on screen vcrec asks the user for a string and sends it to vcsend vcsend echos the received string on screen Repeat steps 1-4 until the user enters a period by itself in either vcsend (step 1) or vcrec (step When the user enters a period, close the connection, and exit both vcrec and vcsend
vcsend code:
#include
/* We must give the hostname and portnumber on the command line. This * checks that the user has typed both. */ if (argc < 3) { printf("error: Invoke this program via: "); printf(" vcsend
{ printf("enter line> "); /* Read input from user */ fgets(chrline, sizeof(chrline) - 1, stdin); /* Check for the period to end the connection and also convert any * newlines into null characters */ for (i = 0 ; i < (sizeof(chrline) - 1) ; i++) { if ( (chrline[i] == ' ') || (chrline[i] == '\0') ) break; } if (chrline[i] == ' ') chrline[i] = '\0'; /* get rid of newline */ length = strlen(chrline); if (chrline[0] == '.') /* end of stream */ { printf("sender: termination requested "); break; /* Exit out of loop */ } ret = send(msgsock, chrline, length + 1, 0); if (ret < 0) { perror("sender: write() failed. "); break; /* Exit out of loop */ } } /* Closing the data socket from vcsend will trigger recv() on vcrec to * return 0, which will also cause vcrec to close its sockets and exit */ close(msgsock); return (0); } /* end of main */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
