linux socket程式設計初步(2)

pengfoo發表於2012-03-14

服務端程式碼:

#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;
}



 

相關文章