Первая проблема, которая возникает, когда я получаю число целых чисел на стороне сервера, возвращает неправильное значение.Вторая проблема заключается в том, что я не получаю правильные значения элементов массива на стороне сервера.Любая помощь будет оценена.
Вот мой код сервера:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define SOCK_PATH "echo_socket"
int main(void)
{
int s, s2, t, len;
struct sockaddr_un local, remote;
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
len = strlen(local.sun_path) + sizeof(local.sun_family);
if (bind(s, (struct sockaddr *)&local, len) == -1) {
perror("bind");
exit(1);
}
if (listen(s, 5) == -1) {
perror("listen");
exit(1);
}
for(;;) {
int done, n;
printf("Waiting for a connection...\n");
t = sizeof(remote);
if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) {
perror("accept");
printf("Check Failed\n"); // to check failed
exit(1);
}
printf("Sequence Ok\n"); // to sequence ok
done = 0;
do {
int nums;
//Server receive number of integers
int numbers = recv(s2, &nums, nums,0);
int str[numbers];
printf("###Server side: Array length is:%d\n",numbers);
//Server receive elements of array
int array = recv(s2,&str,numbers* sizeof(int) ,0);
for(int i=0;i<numbers;i++)
{
printf("###Server side: Array element is:%d\n",str[i]);
}
int tmp=0;
int avg=0;
for(int i=0; i<*str; i++)
{
tmp+=str[i];
printf("tmp is:%d\n",&tmp);
}
avg = tmp / str;
printf("Average of numbers is:%d\n",avg);
if(avg>10)
{
//send(s2, str, n, 0);
}
if (n==1) done = 1;
}
while (!done);
close(s2);
}
return 0;
}
И код клиента:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <ctype.h>
#include <stdbool.h>
#include <unistd.h>
#define SOCK_PATH "echo_socket"
int main(void)
{
int s, t, len, nums, done;
struct sockaddr_un remote;
int str[100];
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
printf("Trying to connect...\n");
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(s, (struct sockaddr *)&remote, len) == -1)
{
perror("connect");
printf("Sequence failed\n");
exit(1);
}
printf("Sequence Ok.\n");
done = 0;
do
{
printf("enter how many numbers you want to insert: ");
scanf("%d",&nums);
int array[nums];
printf("Size of array is:%d\n",nums);
for(int i=0; i<nums; i++)
{
scanf("%d",&array[i]);
printf("Array[%d] = %d\n",i,array[i]);
}
//Send numbers of integers to server
if (send(s, &nums, nums, 0) < 0)
{
puts("Send failed");
return 1;
}
//Send array elements to server
if (send(s, &array, nums* sizeof(int),0) < 0)
{
puts("Send failed");
return 1;
}
if (t==1) done = 1;
} while (!done);
close(s);
return 0;
}
Вывод: Серверответ на стороне:
Waiting for a connection...
Sequence Ok
Server side: Array length is:25
Server side: Array element is:0
Server side: Array element is:0
Server side: Array element is:1994149667
Server side: Array element is:32735
Server side: Array element is:1995560800
Server side: Array element is:32735
Server side: Array element is:-916045749
Server side: Array element is:21898
Server side: Array element is:11
Server side: Array element is:0
Server side: Array element is:1995560800
Server side: Array element is:32735
Server side: Array element is:-916045749
Server side: Array element is:21898
Server side: Array element is:1994101170
Server side: Array element is:32735
Server side: Array element is:0
Server side: Array element is:0
Server side: Array element is:-1230801088
Server side: Array element is:32765
Server side: Array element is:-1230800736
Server side: Array element is:32765
Server side: Array element is:-916049664
Server side: Array element is:21898
Server side: Array element is:-1230800512
Average of numbers is:0
Ответ на стороне клиента:
Sequence Ok.
enter how many numbers you want to insert max(100)> 5
Size of array is:5
1
Array[0] = 1
2
Array[1] = 2
3
Array[2] = 3
4
Array[3] = 4
5
Array[4] = 5
Ожидаемый результат:
Ответ на стороне сервера:
Waiting for a connection...
Sequence Ok
Server side: Array length is:5
Server side: Array element is:1
Server side: Array element is:2
Server side: Array element is:3
Server side: Array element is:4
Server side: Array element is:5
Average of numbers is:3
Ответ со стороны клиента:
Sequence Ok.
enter how many numbers you want to insert max(100)> 5
Size of array is:5
1
Array[0] = 1
2
Array[1] = 2
3
Array[2] = 3
4
Array[3] = 4
5
Array[4] = 5