[转] Socket通信实例

Posted outs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[转] Socket通信实例相关的知识,希望对你有一定的参考价值。

点击阅读原文

Client端:

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

int main(int argc,char **argv)
{
    //char *servInetAddr = "10.68.6.28";
    int socketfd;
    struct sockaddr_in sockaddr;
    char recvline[MAXLINE], sendline[MAXLINE];
    int n;

    if(argc != 2)
    {
        printf("client <ipaddress> 
");
        exit(0);
    }
    
    printf("argv[0]=%s, argv[1]=%s
", argv[0], argv[1]);

    socketfd = socket(AF_INET,SOCK_STREAM,0);
    memset(&sockaddr,0,sizeof(sockaddr));
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(10004);
    //inet_pton(AF_INET,servInetAddr,&sockaddr.sin_addr);
    inet_pton(AF_INET,argv[1],&sockaddr.sin_addr);
    
    if((connect(socketfd,(struct sockaddr*)&sockaddr,sizeof(sockaddr))) < 0 )
    {
        printf("connect error %s errno: %d
",strerror(errno),errno);
        exit(0);
    }

    printf("send message to server
");

    fgets(sendline,1024,stdin);

    if((send(socketfd,sendline,strlen(sendline),0)) < 0)
    {
        printf("send mes error: %s errno : %d",strerror(errno),errno);
        exit(0);
    }

    close(socketfd);
    printf("exit
");
    exit(0);
}

编译client:

gcc client.c -o client

server端:

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

#define MAXLINE 1024

int main(int argc,char **argv)
{
    int listenfd,connfd;
    struct sockaddr_in sockaddr;
    char buff[MAXLINE];
    int n;

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

    sockaddr.sin_family = AF_INET;
    sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    sockaddr.sin_port = htons(10004);

    listenfd = socket(AF_INET,SOCK_STREAM,0);

    bind(listenfd,(struct sockaddr *) &sockaddr,sizeof(sockaddr));

    listen(listenfd,1024);


    printf("Please wait for the client information
");

    for(;;)
    {
        if((connfd = accept(listenfd,(struct sockaddr*)NULL,NULL))==-1)
        {
            printf("accpet socket error: %s errno :%d
",strerror(errno),errno);
            continue;
        }

        n = recv(connfd,buff,MAXLINE,0);
        buff[n] = '';
        printf("recv msg from client:%s",buff);
        close(connfd);
    }
    close(listenfd);
}

编译server:

gcc server.c -o server

测试:

[email protected]:~/Test/socket$ ./server &
[1] 24041
[email protected]:~/Test/socket$ Please wait for the client information

[email protected]:~/Test/socket$ ./client 127.0.0.1
send message to server // client 输出内容
Hello world! // client 输出内容
exit
recv msg from client:Hello world! // server 输出内容
[email protected]:~/Test/socket$

以上是关于[转] Socket通信实例的主要内容,如果未能解决你的问题,请参考以下文章

Java 实例演示-ServerSocket 和 Socket 通信实例

一起talk C栗子吧(第一百五十五回:C语言实例--获取socket通信地址)

socket网口通信通用实例

(转)socket通信_笔记

网络协议栈学习socket通信实例

.net平台下C#socket通信(转)