用C套接字程序不能编译 [英] Socket program in C cannot compile

查看:107
本文介绍了用C套接字程序不能编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正在读一本关于socket编程并有两个codeS客户端和服务器。

Hi guys I was reading a book about the socket programming and there were two codes client and server.

下面是服务器code

here is the server code

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
    perror(msg);
    exit(1);
}
int main(int argc, char *argv[])
{
    int sockfd, newsockfd, portno, clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    if (argc < 2) {
        fprintf(stderr,"ERROR, no port provided/n");
        exit(1);
    }
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
        error("ERROR OPENING SOCKET");
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) <0)
        error("ERROR on binding");
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &client);
    if(newsockfd < 0)
        error("ERROR ON ACCEPT");
    bzero(buffer,256);
    n = read(newsockfd,buffer,255);
    if(n < 0)
        error("ERROR READING FROM SOCKET");
        printf("HERE IS THE MESSAGE: %s\n",buffer);
        n = write(newsockfd,"I GOT YOUR MESSAGE",18);
        if(n < 0)
            error("ERROR WRITING TO SOCKET");
        return 0;
}

和这里的客户端code

and here is the client code

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void error(char *msg)
{
    perror(msg);
    exit(1);
}
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]);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
        error("ERROR OPENING SOCKET");
    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;
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(portno);
    if(connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR CONECTING");
    printf("Please Enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if(n < 0)
    error("ERROR READING THE SOCKET");
    printf("%s\n",buffer);
    return 0;
}

但是,当我与Visual Studio或涡轮C ++或用Borland C编译++,他们给了我的错误,我已经下载了所有必需的头,但问题依然存在。

But when I compile it with visual studio or turbo c++ or borland c++ they gives me error I have downloaded all the required headers but the problem is still there.

推荐答案

现在的问题是,你正在阅读的标准BSDsocket库的例子。 Windows使用一个稍微不同的socket库。

The problem is you are reading examples with the standard "BSD" socket library. Windows uses a slightly different socket library.

有关更通用的方法,可以口标准的BSD套接字程序(你在你的书阅读的内容)到的的Winsock 咨询这里

For a more general approach, you can port standard BSD sockets programs (what you are reading in your book) to Winsock. Advice here.

或者,如果你想在Windows套接字兼容的系统,您可以从 http://cygwin.com/并从那里运行程序。 (请务必下载GCC编译器等中的setup.exe程序。)

Or, if you want a socket compatible system in Windows, you can download Cygwin from http://cygwin.com/ and run your program from there. (Be sure to download the GCC compiler etc in the setup.exe program.)

这篇关于用C套接字程序不能编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆