客户端套接字不会在聊天客户端中使用socket()函数打开

时间:2012-11-14 03:56:42

标签: c linux sockets client chat

所以这是一项正在进行中的工作,但是当我运行命令时: ./server 4444 从另一个终端窗口,。/ client localhost 4444

客户端在“错误打开套接字”处停止。 我不知道为什么,任何建议都将不胜感激。我完全清楚这段代码是错误的和不完整的,但是如果没有让我的客户端和服务器正确连接,我就无法继续前进。

这是我的代码:

client.c

#include "../lib/sockettalk.h"
#include "../lib/my.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>

//#define MAX_SIZE=255

int main(int argc, char **argv)
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);//only need one socket since you are the only one connecting
int n;
char buffer[MAX_SIZE];
struct sockaddr_in serv_addr;
struct hostent *server;
int portnum = atoi(argv[2]);
if(argc < 3)
{
    printf("Check your arguments.");
    exit(0);
}

//printf("%d", portnum);
if(portnum < 2000 || portnum > 65535)//must be greater than 5000 to avoid conflictin ports
{
    printf("Error, port number is out of bounds. Must be >2000 AND <65535.");
    printf("Please enter a valid port number next time.");
    exit(0);
}

if(sockfd < 0);
    printf("Error opening socket. \n");
else
    printf("Opened socket directly. \n");

if((server = gethostbyname(argv[1])) == 0)
{
    printf("Error, no host. \n");
    exit(0);
}

//memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htonl(portnum); 

if((connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) < 0)
{
    printf("Error connecting. \n");

}

while(1)
{
    //clear and write a message
    memset(buffer, '0', sizeof(buffer));
    if((n = write(sockfd, buffer, my_strlen(buffer))) < 0)
    {
        printf("Error writing to server. \n");
        exit(1);
    }
    printf("Client: ");
    printf("%s", buffer);
    printf(" \n");

    //clear and read
    memset(buffer, '0', sizeof(buffer));
    if((n = read(sockfd, buffer, MAX_SIZE)) < 0)
    {
        printf("Error reading from server. \n");
        exit(1);
    }
    printf("Server: ");
    printf("%s", buffer);
    printf(" \n");
}
return 0;
}

server.c

#include "../lib/my.h"
#include "../lib/sockettalk.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>


int main(int argc, char **argv)
{
int port = atoi(argv[1]);
int pid;
int n; //to read and write 
char buffer[MAX_BUFFER_SIZE];
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
int newsockfd;
int clilen; //to be used in accept step as a dummy, need size of bytes
struct sockaddr_in cli_addr, serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));

if (sockfd<0)
    printf("Problem openning socket.\n");
else
{
    printf("Openned socket successfully.\n");
}

serv_addr.sin_family = AF_INET;  //address family
serv_addr.sin_port = port; //IP port
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //IP address, INADDR_ANY allows program to work without knowing the IP address of machine it is running on.

if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)//assigns a name to a socket. we must also cast serv_addr to a pointer to a struct socketDDR
{   
    printf("Error binding socket. \n");
    exit(1);
}
else
{
    printf("Bound socket successfully. \n");
}

if(listen(sockfd, 5) < 0) //should be an already bound socket. 5 is the number of clients that can connect at once
{
    printf("Error listning.\n");
    exit(1);
}

while(1) //place into loop so that process can repeat for every new connection
{
//now a newsockfd needs to be created so that it gives every new client a unique identifir
    newsockfd = accept(sockfd,(struct sockaddr*) &cli_addr, &clilen);
    if(newsockfd < 0)
    {
        printf("Error accepting\n");
        exit(1);
    }
    else
    {
        printf("Client is connected.\n");
    }

    if ((pid = fork()) < 0) //problem
    {
        printf("Error forking. \n");
        exit(1);
    }
    else if(pid == 0) //forking is sucessful, is a child
    {
        close(sockfd); //closes from the child side
        while(1)
        {
            memset(buffer, 0, sizeof(buffer));
            n = read(newsockfd, buffer, MAX_BUFFER_SIZE);
            if (n < 0)
            {
                printf("Error reading from client.\n");
                exit(1);
            }
            printf("%s", buffer);
            printf("\n");

            memset(buffer, 0, sizeof(buffer));
            fgets(buffer, 254, stdin);
            n = write(newsockfd, buffer, strlen(buffer));
            if(n < 0)
            {
                printf("Error writing to client.\n");
                exit(1);
            }
            printf("Server: ");
            printf("%s", buffer);
            printf("\n");
        }
        close(sockfd);
    }       
}
return 0;
}

1 个答案:

答案 0 :(得分:2)

在客户端中,您实际上并未在任何地方使用server = gethostbyname(...)的结果。实际上,你正在离开serv_addr.sin_addr未初始化,所以你试图连接到一些随机IP地址(可能是0.0.0.0),这当然是失败的。

相关问题