os环境下,使用bsd实现echo server并如何获取本地网络信息

2 篇文章 0 订阅

参考:

BSD Socket~TCP~Example Code

BSD Sockets API by MIT

STACK OVER FLOW


Server.c
/*
 * Setting up a simple TCP server involves the following steps:
 *
 * Creating a TCP socket, with a call to socket().
 * Binding the socket to the listen port, with a call to bind(). Before calling bind(), a programmer must declare a sockaddr_in structure, clear it (with memset()), and the sin_family (AF_INET), and fill its sin_port (the listening port, in network byte order) fields. Converting a short int to network byte order can be done by calling the function htons() (host to network short).
 * Preparing the socket to listen for connections (making it a listening socket), with a call to listen().
 * Accepting incoming connections, via a call to accept(). This blocks until an incoming connection is received, and then returns a socket descriptor for the accepted connection. The initial descriptor remains a listening descriptor, and accept() can be called again at any time with this socket, until it is closed.
 * Communicating with the remote host, which can be done through send() and recv() or write() and read().
 * Eventually closing each socket that was opened, once it is no longer needed, using close().
 * Code may set up a TCP server on port 1100 as follows:
 * */

/* Server code in C */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
        printf("server start\n");
        struct sockaddr_in stSockAddr;
        int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

        if(-1 == SocketFD)
        {
                perror("can not create socket");
                exit(EXIT_FAILURE);
        }

        memset(&stSockAddr, 0, sizeof(stSockAddr));

        stSockAddr.sin_family = AF_INET;
        stSockAddr.sin_port = htons(12000);
        stSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);

        printf("bind\n");
        if(-1 == bind(SocketFD,(struct sockaddr *)&stSockAddr, sizeof(stSockAddr)))
        {
                perror("error bind failed");
                close(SocketFD);
                exit(EXIT_FAILURE);
        }
        printf("bind suc\n");

        printf("listen\n");
        if(-1 == listen(SocketFD, 10))
        {
                perror("error listen failed");
                close(SocketFD);
                exit(EXIT_FAILURE);
        }
        printf("listen suc\n");

        for(;;)
        {
                int ConnectFD = accept(SocketFD, NULL, NULL);

                if(0 > ConnectFD)
                {
                        perror("error accept failed");
                        close(SocketFD);
                        exit(EXIT_FAILURE);
                }

                /* perform read write operations ... 
                 *       read(ConnectFD,buff,size)*/

                if (-1 == shutdown(ConnectFD, SHUT_RDWR))
                {
                        perror("can not shutdown socket");
                        close(ConnectFD);
                        close(SocketFD);
                        exit(EXIT_FAILURE);
                }
                close(ConnectFD);
        }

        close(SocketFD);
        return EXIT_SUCCESS;  
}

Client.c
/*
 * Programming a TCP client application involves the following steps:
 *
 * Creating a TCP socket, with a call to socket().
 * Connecting to the server with the use of connect(), passing a sockaddr_in structure with the sin_family set to AF_INET, sin_port set to the port the endpoint is listening (in network byte order), and sin_addr set to the IP address of the listening server (also in network byte order.)
 * Communicating with the server by using send() and recv() or write() and read().
 * Terminating the connection and cleaning up with a call to close().
 * */

/* Client code in C */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
        struct sockaddr_in stSockAddr, foo;
        int len = sizeof(struct sockaddr);
        int Res;
        int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

        if (-1 == SocketFD)
        {
                perror("cannot create socket");
                exit(EXIT_FAILURE);
        }

        memset(&stSockAddr, 0, sizeof(stSockAddr));

        stSockAddr.sin_family = AF_INET;
        stSockAddr.sin_port = htons(12000);
        Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);

        if (0 > Res)
        {
                perror("error: first parameter is not a valid address family");
                close(SocketFD);
                exit(EXIT_FAILURE);
        }
        else if (0 == Res)
        {
                perror("char string (second parameter does not contain valid ipaddress)");
                close(SocketFD);
                exit(EXIT_FAILURE);
        }

        if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr)))
        {
                perror("connect failed");
                close(SocketFD);
                exit(EXIT_FAILURE);
        }

        /* perform read write operations ... */
        getsockname(SocketFD, (struct stSockAddr *) &foo, &len);
        fprintf(stderr, "listening on %s:%d\n", inet_ntoa(foo.sin_addr), 
                                    ntohs(foo.sin_port));

        (void) shutdown(SocketFD, SHUT_RDWR);

        close(SocketFD);
        return EXIT_SUCCESS;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值