我有一个来自班级的旧程序,它是 1 个客户端、1 个服务器,用 c 语言编写了超级基本的“聊天”程序,代码如下。我现在正在尝试为 angstrom 编译它(在 ARM 芯片上运行),并且遇到了这些错误。我安装了 angstrom 工具链,我是否需要为 arm 制作的特殊版本的 sys/socket.h(或不同的标头,忘记读/写/关闭功能位于哪个标头)等?
错误:
~/Desktop/test $ arm-angstrom-linux-gnueabi-g++ main.c -o main -lnsl
main.c: In function 'int main()':
main.c:66: error: invalid conversion from 'int*' to 'socklen_t*'
main.c:66: error: initializing argument 3 of 'int accept(int, sockaddr*, socklen_t*)'
main.c:75: error: 'read' was not declared in this scope
main.c:77: error: 'write' was not declared in this scope
main.c:79: error: 'close' was not declared in this scope
main.c:81: error: 'unlink' was not declared in this scope
我直接从类中复制了代码,所以第一个错误可能只是一些愚蠢的东西,但最后一对说他们甚至找不到我需要的很多功能......但后来它以某种方式识别了接受命令,并且告诉我我使用了错误的论点:/有什么想法吗?
代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h> /* define socket */
#include <netinet/in.h> /* define internet socket */
#include <netdb.h> /* define internet socket */
#define SERVER_PORT 9999 /* define a server port number */
int main()
{
int sd, ns, k;
struct sockaddr_in server_addr = { AF_INET, htons( SERVER_PORT ) };
struct sockaddr_in client_addr = { AF_INET };
int client_len = sizeof( client_addr );
char buf[512], *host;
/* create a stream socket */
if( ( sd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
{
perror( "server: socket failed" );
exit( 1 );
}
/* bind the socket to an internet port */
if( bind(sd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1 )
{
perror( "server: bind failed" );
exit( 1 );
}
/* listen for clients */
if( listen( sd, 1 ) == -1 )
{
perror( "server: listen failed" );
exit( 1 );
}
printf("SERVER is listening for clients to establish a connection\n");
if( ( ns = accept( sd, (struct sockaddr*)&client_addr,
&client_len ) ) == -1 )
{
perror( "server: accept failed" );
exit( 1 );
}
printf("accept() successful.. a client has connected! waiting for a message\n");
/* data transfer on connected socket ns */
while( (k = read(ns, buf, sizeof(buf))) != 0)
{ printf("SERVER RECEIVED: %s\n", buf);
write(ns, buf, k);
}
close(ns);
close(sd);
unlink(server_addr.sin_addr);
return(0);
}