Skip to content
Snippets Groups Projects
Commit ce303937 authored by 20041679's avatar 20041679
Browse files

echo server advanced

parent c16f267b
No related branches found
No related tags found
No related merge requests found
......@@ -21,57 +21,67 @@ int main(int argc, char *argv[]) {
exit(1);
}
while (1) {
/* create a streaming socket */
simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
/* create a streaming socket */
simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (simpleSocket == -1) {
if (simpleSocket == -1) {
fprintf(stderr, "Could not create a socket!\n");
exit(1);
fprintf(stderr, "Could not create a socket!\n");
exit(1);
}
else {
fprintf(stderr, "Socket created!\n");
}
}
else {
fprintf(stderr, "Socket created!\n");
}
/* retrieve the port number for connecting */
simplePort = atoi(argv[2]);
/* retrieve the port number for connecting */
simplePort = atoi(argv[2]);
/* setup the address structure */
/* use the IP address sent as an argument for the server address */
//bzero(&simpleServer, sizeof(simpleServer));
memset(&simpleServer, '\0', sizeof(simpleServer));
simpleServer.sin_family = AF_INET;
//inet_addr(argv[2], &simpleServer.sin_addr.s_addr);
simpleServer.sin_addr.s_addr=inet_addr(argv[1]);
simpleServer.sin_port = htons(simplePort);
/* setup the address structure */
/* use the IP address sent as an argument for the server address */
//bzero(&simpleServer, sizeof(simpleServer));
memset(&simpleServer, '\0', sizeof(simpleServer));
simpleServer.sin_family = AF_INET;
//inet_addr(argv[2], &simpleServer.sin_addr.s_addr);
simpleServer.sin_addr.s_addr=inet_addr(argv[1]);
simpleServer.sin_port = htons(simplePort);
/* connect to the address and port with our socket */
returnStatus = connect(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer));
/* connect to the address and port with our socket */
returnStatus = connect(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer));
if (returnStatus == 0) {
fprintf(stderr, "Connect successful!\n");
}
else {
fprintf(stderr, "Could not connect to address!\n");
close(simpleSocket);
exit(1);
}
if (returnStatus == 0) {
fprintf(stderr, "Connect successful!\n");
}
else {
fprintf(stderr, "Could not connect to address!\n");
close(simpleSocket);
exit(1);
}
puts("numero interazioni");
char buffer[256];
fgets(buffer, ARRAY_SIZE(buffer), stdin);
write(simpleSocket, buffer, strlen(buffer));
puts("numero interazioni");
char buffer[256];
fgets(buffer, ARRAY_SIZE(buffer), stdin);
write(simpleSocket, buffer, strlen(buffer));
returnStatus = read(simpleSocket, buffer, sizeof(buffer));
// no error checking on client
long n_iterations = atol(buffer);
for (long i = 0; i < n_iterations; ++i) {
fgets(buffer, ARRAY_SIZE(buffer), stdin);
if (write(simpleSocket, buffer, strlen(buffer)) < 0) {
break;
}
ssize_t returnStatus = read(simpleSocket, buffer, sizeof(buffer));
if (returnStatus < 0) {
break;
}
if (returnStatus > 0) {
buffer[ARRAY_SIZE(buffer) - 1] = '\0';
printf("%s", buffer);
buffer[returnStatus - 1] = '\0';
puts(buffer);
}
close(simpleSocket);
}
close(simpleSocket);
return 0;
}
......@@ -10,6 +10,19 @@
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
int parse_long(char *s, long *ret) {
if (!ret) {
return -1;
}
char *end = s;
long valid = strtol(s, &end, 10);
if (!end) {
return -1;
}
*ret = valid;
return 0;
}
int main(int argc, char *argv[]) {
int simpleSocket = 0;
int simplePort = 0;
......@@ -78,16 +91,37 @@ int main(int argc, char *argv[]) {
puts("connection accepted");
char buffer[256];
// reading n_iterations
ssize_t returnStatus = read(simpleChildSocket, buffer, sizeof(buffer));
if (returnStatus < 0) {
continue;
if (returnStatus <= 0) {
goto CLOSE;
}
if (returnStatus > 0) {
buffer[ARRAY_SIZE(buffer) - 1] = '\0';
printf("read %s", buffer);
buffer[returnStatus - 1] = '\0';
printf("read %s\n", buffer);
long n_iterations;
if (parse_long(buffer, &n_iterations) < 0) {
puts("invalid number");
goto CLOSE;
}
write(simpleChildSocket, buffer, returnStatus);
for (long i = 0; i < n_iterations; ++i) {
ssize_t returnStatus = read(simpleChildSocket, buffer, sizeof(buffer));
if (returnStatus < 0) {
break;
}
printf("read ");
if (returnStatus > 0) {
buffer[returnStatus - 1] = '\0';
printf("%s\n", buffer);
} else {
puts("nothing");
}
if (write(simpleChildSocket, buffer, returnStatus) < 0) {
break;
}
}
CLOSE:
close(simpleChildSocket);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment