gpt4 book ai didi

c - 套接字编程客户端/服务器

转载 作者:太空宇宙 更新时间:2023-11-04 08:50:09 25 4
gpt4 key购买 nike

我是套接字编程的新手,正在尝试弄清楚如何在客户端和服务器之间建立简单的连接。

我找到了一个解释它的网站。 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 port\n", 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 host\n");
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("%s\n",buffer);
return 0;
}

我有点迷茫,需要帮助。提前致谢

最佳答案

不是一个完整的答案,但我的建议是用 more warnings turned on (e.g. with -Wall) 编译:

$ gcc -Wall -o client client.cclient.c: In function `main':client.c:16: warning: implicit declaration of function `exit'client.c:18: warning: implicit declaration of function `atoi'client.c:26: warning: implicit declaration of function `gethostbyname'client.c:26: warning: assignment makes pointer from integer without a castclient.c:32: warning: implicit declaration of function `bzero'client.c:34: warning: implicit declaration of function `bcopy'client.c:34: error: dereferencing pointer to incomplete typeclient.c:34: error: dereferencing pointer to incomplete typeclient.c:38: warning: passing arg 2 of `connect' from incompatible pointer typeclient.c:50: warning: implicit declaration of function `write'client.c:50: warning: implicit declaration of function `strlen'client.c:58: warning: implicit declaration of function `read'$ 

This makes a lot of the problems much clearer.

For all the implicit declaration of function warnings, just look up the relevant manpage to find what header you need:

$ 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 部分中,但您可能需要四处寻找。

$ man man | grep calls       2   System calls (functions provided by the kernel)       3   Library calls (functions within program libraries)$

关于c - 套接字编程客户端/服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19969889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com