谁能告诉我如何将整数从客户端发送到服务器并将它们添加到c中。
我能够成功发送字符串,但我无法弄清楚如何发送整数。
请帮帮我!!下面编写的代码用于读取字符串。我怎样才能改变它来读取和添加整数。
#define SOCK_PATH "echo_socket"
int main(void)
{
int s, t, len;
struct sockaddr_un remote;
char 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);
int val=connect(s, (struct sockaddr *)&remote, len);
if ( val< 0) {
perror("connect");
exit(1);
}
printf("Connected.\n");
printf("ENTER THE NUMBERS:");
while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
if (send(s, str, strlen(str), 0) == -1) {
perror("send");
exit(1);
}
if ((t=recv(s, str, 100, 0)) > 0) {
str[t] = '\0';
printf("echo> %s", str);
} else
{
if (t < 0) perror("recv");
else printf("Server closed connection\n");
exit(1);
}
}