码迷,mamicode.com
首页 > 其他好文 > 详细

socket

时间:2015-08-27 18:09:57      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

server

 

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

#define MAXLINE 4096

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

    if( ( listenfd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
    {
        printf( "create socket error: %s(errno: %d)\n",strerror( errno ),errno );
        exit( 0 );
    }

    memset( &servaddr, 0, sizeof( servaddr ) );
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
    servaddr.sin_port = htons( 6666 );

    if( bind( listenfd, ( struct sockaddr* )&servaddr, sizeof( servaddr ) ) == -1 )
    {
        printf( "bind socket error: %s(errno: %d)\n",strerror( errno ),errno );
        exit( 0 );
    }

    if( listen( listenfd, 10 ) == -1 )
    {
        printf( "listen socket error: %s(errno: %d)\n",strerror( errno ),errno );
        exit( 0 );
    }

    printf( "======waiting for client‘s request======\n" );
    while( 1 )
    {
        if( ( connfd = accept( listenfd, ( struct sockaddr* )NULL, NULL ) ) == -1 )
        {
            printf( "accept socket error: %s(errno: %d)",strerror( errno ),errno );
            continue;
        }
        n = recv( connfd, buff, MAXLINE, 0 );
        buff[n] = \0;
        printf( "recv msg from client: %s\n", buff );
        close( connfd );
    }

    close( listenfd );
}

 

client

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

#define MAXLINE 4096

int main( int argc, char** argv )
{
    int sockfd, n;
    char recvline[4096], sendline[4096];
    struct sockaddr_in servaddr;
#if 0
    if( argc != 2 )
    {
        printf( "usage: ./client <ipaddress>\n" );
        exit( 0 );
    }
#endif
    if( ( sockfd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
    {
        printf( "create socket error: %s(errno: %d)\n", strerror( errno ),errno );
        exit( 0 );
    }

    memset( &servaddr, 0, sizeof( servaddr ) );
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr( "109.123.123.36" );
    servaddr.sin_port = htons( 6666 );

#if 0
    if( inet_pton( AF_INET, argv[1], &servaddr.sin_addr ) <= 0 )
    {
        printf( "inet_pton error for %s\n",argv[1] );
        exit( 0 );
    }
#endif

    if( connect( sockfd, ( struct sockaddr* )&servaddr, sizeof( servaddr ) ) < 0 )
    {
        printf( "connect error: %s(errno: %d)\n",strerror( errno ),errno );
        exit( 0 );
    }

    printf( "send msg to server: \n" );
    fgets( sendline, 4096, stdin );
    if( send( sockfd, sendline, strlen( sendline ), 0 ) < 0 )
    {
        printf( "send msg error: %s(errno: %d)\n", strerror( errno ), errno );
        exit( 0 );
    }

    close( sockfd );
    exit( 0 );
}

 

另一种:

server:

//s_unix.c  
#include <stdio.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <sys/un.h>   
#define UNIX_DOMAIN "/tmp/UNIX.domain"  
int main(void)  
{  
    socklen_t clt_addr_len;  
    int listen_fd;  
    int com_fd;  
    int ret;  
    int i;  
    static char recv_buf[1024];   
    int len;  
    struct sockaddr_un clt_addr;  
    struct sockaddr_un srv_addr;  
    listen_fd=socket(PF_UNIX,SOCK_STREAM,0);  
    if(listen_fd<0)  
    {  
        perror("cannot create communication socket");  
        return 1;  
    }    
      
    //set server addr_param  
    srv_addr.sun_family=AF_UNIX;  
    strncpy(srv_addr.sun_path,UNIX_DOMAIN,sizeof(srv_addr.sun_path)-1);  
    unlink(UNIX_DOMAIN);  
    //bind sockfd & addr  
    ret=bind(listen_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));  
    if(ret==-1)  
    {  
        perror("cannot bind server socket");  
        close(listen_fd);  
        unlink(UNIX_DOMAIN);  
        return 1;  
    }  
    //listen sockfd   
    ret=listen(listen_fd,1);  
    if(ret==-1)  
    {  
        perror("cannot listen the client connect request");  
        close(listen_fd);  
        unlink(UNIX_DOMAIN);  
        return 1;  
    }  
    //have connect request use accept  
    len=sizeof(clt_addr);  
    com_fd=accept(listen_fd,(struct sockaddr*)&clt_addr,&len);  
    if(com_fd<0)  
    {  
        perror("cannot accept client connect request");  
        close(listen_fd);  
        unlink(UNIX_DOMAIN);  
       return 1;  
    }  
    //read and printf sent client info  
    printf("\n=====info=====\n");  
    for(i=0;i<4;i++)  
    {  
        memset(recv_buf,0,1024);  
        int num=read(com_fd,recv_buf,sizeof(recv_buf));  
        printf("Message from client (%d)) :%s\n",num,recv_buf);    
    }  
    close(com_fd);  
    close(listen_fd);  
    unlink(UNIX_DOMAIN);  
    return 0;  
} 

 

client

//c_unix.c  
#include <stdio.h>  
#include <sys/types.h>  
#include <sys/socket.h>  
#include <sys/un.h>  
#define UNIX_DOMAIN "/tmp/UNIX.domain"  
int main(void)  
{  
int connect_fd;  
int ret;  
char snd_buf[1024];  
int i;  
static struct sockaddr_un srv_addr;  
//creat unix socket  
connect_fd=socket(PF_UNIX,SOCK_STREAM,0);  
if(connect_fd<0)  
{  
    perror("cannot create communication socket");  
    return 1;  
}     
srv_addr.sun_family=AF_UNIX;  
strcpy(srv_addr.sun_path,UNIX_DOMAIN);  
//connect server  
ret=connect(connect_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));  
if(ret==-1)  
{  
    perror("cannot connect to the server");  
    close(connect_fd);  
    return 1;  
}  
memset(snd_buf,0,1024);  
strcpy(snd_buf,"message from client");  
//send info server  
for(i=0;i<4;i++)  
    write(connect_fd,snd_buf,sizeof(snd_buf));  
close(connect_fd);  
return 0;  
}  

 

socket

标签:

原文地址:http://www.cnblogs.com/jeakeven/p/4763881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!