Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 套接字编程客户端/服务器_C_Sockets - Fatal编程技术网

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

C 套接字编程客户端/服务器,c,sockets,C,Sockets,我对socket编程还不熟悉,并试图弄清楚如何在客户端和服务器之间建立简单的连接 我找到了一个网站解释了这件事。 我尝试在我的ubuntu终端(gcc-oclient.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:

我对socket编程还不熟悉,并试图弄清楚如何在客户端和服务器之间建立简单的连接

我找到了一个网站解释了这件事。

我尝试在我的ubuntu终端(gcc-oclient.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;
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
int sockfd,端口号,n;
服务地址中的结构sockaddr\u;
结构主机*服务器;
字符缓冲区[256];
如果(argc<3){
fprintf(stderr,“使用%s主机名端口,\n”,argv[0]);
出口(0);
}
portno=atoi(argv[2]);
/*创建一个套接字点*/
sockfd=套接字(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
perror(“错误打开插座”);
出口(1);
}
//警告:赋值从不带强制转换的整数生成指针[由默认值启用]
server=gethostbyname(argv[1]);
如果(服务器==NULL){
fprintf(stderr,“错误,没有这样的主机\n”);
出口(0);
}
bzero((char*)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;

//取消对不完整类型的引用指针 b复制((char*)服务器->h_地址,(char*)和服务地址sin_地址s_地址,服务器->h_长度); serv_addr.sin_port=htons(端口号); /*现在连接到服务器*/ if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0) { perror(“错误连接”); 出口(1); } /*现在向用户请求一条消息,这条消息 *将由服务器读取 */ printf(“请输入消息:”); b0(缓冲器,256); fgets(缓冲区,255,标准输入); /*向服务器发送消息*/ n=写入(sockfd,buffer,strlen(buffer)); if(n<0) { perror(“写入套接字时出错”); 出口(1); } /*现在读取服务器响应*/ b0(缓冲器,256); n=读取(sockfd,缓冲区,255); if(n<0) { perror(“从套接字读取错误”); 出口(1); } printf(“%s\n”,缓冲区); 返回0; }

我有点迷路了,需要帮助。提前感谢

这不是一个完整的答案,但我的建议是使用以下内容进行编译:

通常,这些类型的调用将在手册页的第2或第3节中介绍,但您可能需要四处搜索

$ man man | grep calls 2 System calls (functions provided by the kernel) 3 Library calls (functions within program libraries) $ $man man |格雷普电话 2个系统调用(内核提供的函数) 3个库调用(程序库中的函数) $
取消引用指向不完整类型的指针,意味着您缺少了需要包含的某些头文件。也许您想共享我实际上遗漏了哪些头文件?请查看您在指定行中使用的类型。如果您在代码中添加注释并说“此处是显示的位置”,则会有所帮助,或者突出显示代码中的行号。您至少缺少以下include文件,应该限制代码作者按原样发布代码<我建议使用
-Wall
-Wextra
进行编译。您也可以查看
迂腐的
,但我认为这只适用于
-ansi
$ 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 */
$ 
$ man man | grep calls 2 System calls (functions provided by the kernel) 3 Library calls (functions within program libraries) $