我正在编写一个程序来处理网络套接字而不是unix接口。我不确定它是否是头文件或函数和头文件的使用不正确。程序将向屏幕发送和打印消息,直到用户输入"退出"使用网络套接字。尝试编译此服务器会产生许多错误,即使我的客户端编译了一次。
...更新
错误("主要"以及顶级错误):
server02.c:37:5: error: 'server_socket' undeclared (first use in this
function)
server02.c:37:5: note: each undeclared identifier is reported only once for
each function it appears in
server02.c:41:26: error: 'struct sockaddr_in' has no member named 'sin_path'
server02.c:94:252: error: expected identifier or '(' before '}' token
server02.c:95:253: error: expected identifier or '(' before '}' token
server02.c:98:264: error: expected declaration specifiers or '...' before
string constant
server02.c:99:259: warning: data definition has no type or storage class
[enabled by default]
server02.c:99:259: warning: type defaults to 'int' in declaration of 'close'
[enabled by default]
server02.c:99:259: warning: parameter names (without types) in function
declaration [enabled by default]
server02.c:100:261: warning: data definition has no type or storage class
[enabled by default]
server02.c:100:261: warning: type defaults to 'int' in declaration of
'close' [enabled by default]
server02.c:100:261: warning: parameter names (without types) in function
declaration [enabled by default]
server02.c:101:263: error: expected identifier or '(' before 'return'
server02.c:103:263: error: expected identifier or '(' before '}' token
服务器1的代码(客户端编译)...
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
//#include <sys/un.h> //not needed anymore b/c in, not unix?
#include <unistd.h>
#include <string.h>
//ADDING THESE HELP, BUT STILL ERRORS
#include <netinet/in.h>
#include <netinet/ip.h>
#define PORT 50014
#define LEN 256
#define SOCKET_NAME "server_socket"
int main(void)
{
//some may not be needed
char buf[LEN];
int server_sockfd;
int client_sockfd;
socklen_t client_len;
ssize_t num_client_bytes;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
//create new
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == server_sockfd)
{
perror("server -- socket failed");
exit(EXIT_FAILURE);
}
//bind new
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_socket.sin_addr.s_addr = 0;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(PORT);
strcpy(server_address.sun_path, SOCKET_NAME);//not sure if needed
if(bind(server_sockfd, (struct sockaddr*)&server_address,
sizeof(server_address)))
{
perror("server -- bind failed");
}
//create connection q and wait for client
if(listen(server_sockfd, 5))
{
perror("server-- listen failed");
}
//announce server is waiting for client connection
puts("server waiting...");
//accept connection
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr*)&client_address,
&client_len);
//LOOP AND SEND MESSAGE HERE
//close stuff
close(client_sockfd);
close(server_sockfd);
return 0;
}
答案 0 :(得分:1)
您错过了互联网套接字标题:
#include <netinet/in.h>
#include <netinet/ip.h>
答案 1 :(得分:0)
添加
#include <netinet/in.h>
定义宏
时#define a b
这意味着无论编译器在哪里看到它,它都应该用b代替。在您的代码中,您已经完成了类似的事情
#define SOCKET_NAME "server_socket"
但是什么是 server_socket ?可能你的意思是 server_address ,我在 #bind new 第3行
部分得到这个cusserver_socket.sin_addr.s_addr = 0;
这是正确的语法
server_address.sin_addr.s_addr = 0;
还修复了宏( server_socket ?)
#define SOCKET_NAME "server_socket"