linux socket编程初步(2)

服务端代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/fcntl.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

#define SERVER_PORT 12000
#define BUF_SIZE 4096 /* block transfer size */
#define SIZE 10 /* how many client can connect at the same time */

void fatal(char *string)
{
    printf("%s\n",string);
    exit(1);
}

int main(/*int argc,char * argv[]*/)
{
    printf("main begin!\n");

    int s,b,l,fd,sa,bytes,on=1;
    char buf[BUF_SIZE]; /*buf for outgoing files*/
    struct sockaddr_in channel; /* hold IP address*/

    /* Build address structure to bind to socket. */
    bzero(&channel,sizeof(channel));
    channel.sin_family=AF_INET;
    channel.sin_addr.s_addr=htonl(INADDR_ANY);
    channel.sin_port=htons(SERVER_PORT);

    printf("main begin--2!\n");
    /* Passive open.Wait for connection. */
    s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);  /* Creat socket */
    if(s<0)
        fatal("socket failed");
    else
        printf("socket sucess!!\n");

    setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on));
        /* use the socket for many time (up word) */
    b=bind(s,(struct sockaddr *)&channel,sizeof(channel));
    if(b<0)
        fatal("bind failed");
    else
        printf("bind sucess!!\n");

    l=listen(s,SIZE);
    if(l<0)
        fatal("listen failed");
    else
         printf("listen sucess!!\n");

 /* Socket is now set up and bound. Wait for connection and process it. */

    while(1)
    {
         printf("in while!!\n");
        sa=accept(s,0,0);
        if(sa<0)
            fatal("accept failed");

        bytes=recv(sa,buf,BUF_SIZE,0);

        buf[bytes]='\n';
        write(1,buf,bytes+1);//'1' is the stdout.

        close(fd);
        close(sa);
    }
    return 0;
}

客户端:

/*
 *This page contains a client program that can send a message to the server program.
 */

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

#define SERVER_PORT 12000
#define BUF_SIZE 100
#define DEST_IP "127.0.0.1"//Changed by fupeng

void fatal(char *s)
{
    printf("%s\n",s);
    exit(1);
}

int main(/*int argc,char * *argv*/)

{
    int c,s,bytes;
    char buf[BUF_SIZE];             /*buf for incoming files*/
    struct sockaddr_in channel;     /* hold IP address*/

//    if (argc!=2)
//    {
//        printf("Usage:%s IPaddress\n",argv[0]);
//        exit(1);
//    }

    s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(s<0)
        fatal("socket");
//    else cout<<"socket success!"<<endl;
    else printf("socket success!\n");

    bzero(&channel,sizeof(channel));
    channel.sin_family=AF_INET;
    channel.sin_port=htons(SERVER_PORT);
    if(inet_aton(DEST_IP,&channel.sin_addr)==0)
    {
        /* use the ASCII ip for the  server's sin_addr. */
        fprintf(stderr,"Inet_aton erro\n");
        exit(1);
     }

    c=connect(s,(struct sockaddr *)&channel,sizeof(channel));
    if(c<0)
        fatal("connetct failed");

    char sendline[BUF_SIZE];
    int num;
    printf("connected to server.\n");
    printf("Input the message:");
    if(fgets(sendline,BUF_SIZE,stdin)==NULL)
    {
        printf("\nExit.\n");
        return 1;//changed by fupeng
    }
    num=strlen(sendline);
    sendline[num-1]='\0';
    send(s,sendline,strlen(sendline),0);
    close(s);
    return 0;
}



 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值