C语言的初学者套接字编程 [英] Beginner's Socket Programming in C

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

问题描述

我刚刚开始学习套接字编程,并且发现它非常有趣. 目前,我正在将服务器和客户端设置在同一台计算机上,因此我可以将IP地址作为回送地址127.0.0.1,并且一切似乎都可以正常工作!

I have just started learning socket programming and am finding it pretty interesting. Currently I am making the server and the client on the same computer and hence I can have the ip address as the loopback address, 127.0.0.1 and everything seems to work fine!!

但是现在我正在考虑拥有两台计算机并做这件事. 我有以下问题- 1)假设一台计算机是服务器,另一台是客户端.现在,服务器代码应该驻留在服务器计算机上,客户端代码应该驻留在客户端计算机上吗? 2)在服务器代码中,当我们为bind()提供ip地址时,它应该是我们可以通过ipconfig找到的系统的ip地址,或者它仍应保留为回送地址? 3)在客户端代码中,我猜目标的IP地址应该是服务器计算机的IP地址吗? 4)最后也是最重要的一点,我如何连接两个计算机?

But now I was thinking of having two computers and doing the thing.. I have the following questions - 1) Say one computer is Server and another is Client. Now, should the server code reside on the server computer and the Client code on the client one?? 2) In the server code when we are providing the ip address for bind(), it should be the ip address of the system that we can find through ipconfig or it should still remain the loopback address?? 3) In the client code, I guess the ip address of the destination should be that of the server computer right?? 4) And the last and the most important thing, HOW DO I CONNECT THE TWO COMPUTERS??

我将附上我最初使用的简单服务器和客户端消息传递代码.请指导我完成需要进行的更改.

I am attaching the simple server and client message passing code that I started out with. Kindly guide me through the changes that I need to make..

服务器代码

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

#define MYPORT 3500

int main()
{
    int sockfd;
    int clientfd;
    int bytes_read;
    char buf[100];
    int struct_size;
    struct sockaddr_in my_addr;
    struct sockaddr_in con_addr;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(MYPORT);
    my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    my_addr.sin_zero[8]='\0';

    bind(sockfd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));

    listen(sockfd,5);

    struct_size = sizeof(con_addr);
    clientfd = accept(sockfd, (struct sockaddr*)&con_addr, &struct_size);

    bytes_read = read(clientfd, buf, 100);
    buf[bytes_read] = '\0';
    printf("Message from client:%d is %s \n",clientfd, buf);

    close(sockfd);
    close(clientfd);
}

客户代码

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

#define DESTPORT 3500

int main()
{

    struct sockaddr_in dest_addr;

    int sockfd = socket(AF_INET,SOCK_STREAM,0);

    dest_addr.sin_family = AF_INET;
    dest_addr.sin_port = htons(DESTPORT);
    dest_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    dest_addr.sin_zero[8]='\0';

    connect(sockfd,(struct sockaddr*)&dest_addr, sizeof(struct sockaddr));

    char msg[100];
    printf("Enter you message: ");
    gets(&msg); 

    int w = write(sockfd, msg, strlen(msg));

    close(sockfd);
    printf("Client Dying.....\n"); 

    return 0;
}

推荐答案

服务器应该bind0.0.0.0(任意),除非您试图限制访问(在这种情况下,您应该真正使用防火墙而不是使用防火墙)端口绑定).正确的方法实际上是:

Server should bind to 0.0.0.0 (any) unless you're trying to restrict access (and in that case you should really use a firewall rather than port binding). The correct way is actually:

struct addrinfo *ai, hints = { .ai_flags = AI_PASSIVE };
if (getaddrinfo(0, "1234", &hints, &ai)) goto error;
int fd = socket(ai->ai_family, SOCK_STREAM, 0);
bind(fd, ai->ai_addr, ai->ai_addrlen);

当然要添加一些错误检查.将"1234"替换为您的端口号.

Add some error checking, of course. Replace "1234" with your port number.

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

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