C10=] Echo Server -- 绑定和接受函数出错
C TCP/IP Echo Server -- Error on bind & accept fucntions
我无法绑定套接字。当我编译这段代码时:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Error. Please enter the right amount of arguments (2)");
exit(1);
}
int sock, port, clientlen, connection, readfd, writefd;
char buffer[255];
struct sockaddr_in server, clientaddr;
if (sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) < 0)
{
printf("Socket was not made.");
exit(1);
}
bzero((char *) &server, sizeof(server));
port = atoi(argv[1]);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
if ((bind(socket, (struct sockaddr *) &server, sizeof(server))) < 0)
{
printf("Could not bind socket to address\n");
exit(1);
}
listen(socket, 5);
clientlen = sizeof(clientaddr);
if (connection = accept(socket, (struct sockaddr *) &clientaddr, &clientlen) < 0)
{
printf("Could not accept connection");
exit(1);
}
if (readfd = read(connection, buffer, 255) < 0)
{
printf("Could not read from socket");
exit(1);
}
printf("!---THIS IS A TEST---! \n BUFFER: %s", buffer);
if (writefd = write(connection, buffer, 255) < 0)
{
printf("Could not write to socket");
exit(1);
}
return 0;
}
我收到错误:
a3server.c: In function ‘main’:
a3server.c:33:28: warning: passing argument 1 of ‘bind’ makes integer from pointer without a cast [enabled by default]
if ((bind(socket, (struct sockaddr *) &server, sizeof(server))) < 0)
^
In file included from a3server.c:2:0:
/usr/include/x86_64-linux-gnu/sys/socket.h:123:12: note: expected ‘int’ but argument is of type ‘int (*)(int, int, int)’
extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
^
a3server.c:39:2: warning: passing argument 1 of ‘listen’ makes integer from pointer without a cast [enabled by default]
listen(socket, 5);
^
In file included from a3server.c:2:0:
/usr/include/x86_64-linux-gnu/sys/socket.h:233:12: note: expected ‘int’ but argument is of type ‘int (*)(int, int, int)’
extern int listen (int __fd, int __n) __THROW;
^
a3server.c:43:42: warning: passing argument 1 of ‘accept’ makes integer from pointer without a cast [enabled by default]
if (connection = accept(socket, (struct sockaddr *) &clientaddr, &clientlen) < 0)
^
In file included from a3server.c:2:0:
/usr/include/x86_64-linux-gnu/sys/socket.h:243:12: note: expected ‘int’ but argument is of type ‘int (*)(int, int, int)’
extern int accept (int __fd, __SOCKADDR_ARG __addr,
我在这里遵循了这个指南:http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html但我不太确定哪里出错了。
此外,这是我在编译时的命令 "gcc a3server.c -g -o server" 如果这与它有任何关系。
您在 bind()、listen() 和 accept() 中使用的 'socket' 变量未定义。 socket() 的文件描述符 return 保存在 'sock' 变量中。您必须将出现的 'socket' 替换为 'sock'。
正确的做法是:
bind (sock, (struct sockaddr *) &server, sizeof (server))
...
listen (sock, 5);
...
accept (sock, ...
我无法绑定套接字。当我编译这段代码时:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Error. Please enter the right amount of arguments (2)");
exit(1);
}
int sock, port, clientlen, connection, readfd, writefd;
char buffer[255];
struct sockaddr_in server, clientaddr;
if (sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) < 0)
{
printf("Socket was not made.");
exit(1);
}
bzero((char *) &server, sizeof(server));
port = atoi(argv[1]);
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
if ((bind(socket, (struct sockaddr *) &server, sizeof(server))) < 0)
{
printf("Could not bind socket to address\n");
exit(1);
}
listen(socket, 5);
clientlen = sizeof(clientaddr);
if (connection = accept(socket, (struct sockaddr *) &clientaddr, &clientlen) < 0)
{
printf("Could not accept connection");
exit(1);
}
if (readfd = read(connection, buffer, 255) < 0)
{
printf("Could not read from socket");
exit(1);
}
printf("!---THIS IS A TEST---! \n BUFFER: %s", buffer);
if (writefd = write(connection, buffer, 255) < 0)
{
printf("Could not write to socket");
exit(1);
}
return 0;
}
我收到错误:
a3server.c: In function ‘main’:
a3server.c:33:28: warning: passing argument 1 of ‘bind’ makes integer from pointer without a cast [enabled by default]
if ((bind(socket, (struct sockaddr *) &server, sizeof(server))) < 0)
^
In file included from a3server.c:2:0:
/usr/include/x86_64-linux-gnu/sys/socket.h:123:12: note: expected ‘int’ but argument is of type ‘int (*)(int, int, int)’
extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
^
a3server.c:39:2: warning: passing argument 1 of ‘listen’ makes integer from pointer without a cast [enabled by default]
listen(socket, 5);
^
In file included from a3server.c:2:0:
/usr/include/x86_64-linux-gnu/sys/socket.h:233:12: note: expected ‘int’ but argument is of type ‘int (*)(int, int, int)’
extern int listen (int __fd, int __n) __THROW;
^
a3server.c:43:42: warning: passing argument 1 of ‘accept’ makes integer from pointer without a cast [enabled by default]
if (connection = accept(socket, (struct sockaddr *) &clientaddr, &clientlen) < 0)
^
In file included from a3server.c:2:0:
/usr/include/x86_64-linux-gnu/sys/socket.h:243:12: note: expected ‘int’ but argument is of type ‘int (*)(int, int, int)’
extern int accept (int __fd, __SOCKADDR_ARG __addr,
我在这里遵循了这个指南:http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html但我不太确定哪里出错了。
此外,这是我在编译时的命令 "gcc a3server.c -g -o server" 如果这与它有任何关系。
您在 bind()、listen() 和 accept() 中使用的 'socket' 变量未定义。 socket() 的文件描述符 return 保存在 'sock' 变量中。您必须将出现的 'socket' 替换为 'sock'。
正确的做法是:
bind (sock, (struct sockaddr *) &server, sizeof (server))
...
listen (sock, 5);
...
accept (sock, ...