我是套接字编程的新手,并试图弄清楚如何在客户端和服务器之间建立简单的连接。
我找到了一个解释它的网站。http://www.tutorialspoint.com/unix_sockets/socket_client_example.htm
我尝试在我的 ubuntu 终端( gcc -o client client.c) 中执行代码
但是,它会打印出以下错误消息
client.c In function 'main' :
client.c:32:12: warning: assignment makes pointer from integer without a cast[enabled by defaulted]
client.c:40:25: error : dereferencing pointer to incomplete type
client.c:40:23: error : dereferencing pointer to incomplete type
client.c 46:5: warning: passing argument 2 of 'connect' from incompatible pointer type [enabled by default]
In file included from client c:1:0:
/usr/include/x86_64-linux-gnu/sys/socket.h:138:12: note:expected 'const struct sockaddr *' but argument is of type 'stuct sockaddr_in'
来自网站的client.c代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname portn", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
/* Create a socket point */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
exit(1);
}
//warning: assignment makes pointer from integer without a cast[enabled by defaulted]
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such hostn");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
//dereferencing pointer to incomplete type
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
/* Now connect to the server */
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
{
perror("ERROR connecting");
exit(1);
}
/* Now ask for a message from the user, this message
* will be read by server
*/
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
/* Send message to the server */
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
{
perror("ERROR writing to socket");
exit(1);
}
/* Now read server response */
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
{
perror("ERROR reading from socket");
exit(1);
}
printf("%sn",buffer);
return 0;
}
我有点迷茫,需要帮助。 提前谢谢
不是真正的完整答案,但我的建议是在打开更多警告的情况下进行编译(例如使用 -Wall):
$ gcc -Wall -o client client.cclient.c:在函数'main'中:client.c:16:警告:函数"exit"的隐式声明client.c:18:警告:函数"atoi"的隐式声明client.c:26:警告:函数"gethostbyname"的隐式声明client.c:26:警告:赋值从整数生成指针而不进行强制转换client.c:32:警告:函数"bzero"的隐式声明client.c:34:警告:函数"bcopy"的隐式声明client.c:34:错误:取消引用指向不完整类型的指针client.c:34:错误:取消引用指向不完整类型的指针client.c:38:警告:从不兼容的指针类型传递"连接"的参数 2client.c:50:警告:函数"write"的隐式声明client.c:50:警告:函数"strlen"的隐式声明client.c:58:警告:函数"read"的隐式声明$
这使得许多问题更加清晰。
对于所有implicit declaration of function
警告,只需查找相关的手册页即可找到所需的标题:
$ man -s3 exit | grep '#include'
#include <stdlib.h>
$ man -s2 write | grep '#include'
#include <unistd.h>
$ man -s3 gethostbyname | grep '#include'
#include <netdb.h>
#include <sys/socket.h> /* for AF_INET */
$
通常,这些类型的调用将位于手册页的第 2 或第 3 节中,但您可能需要四处寻找。
$ 男人 |格雷普调用 2 系统调用(内核提供的函数) 3 库调用(程序库中的函数)$