Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28983
  • 博文数量: 17
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-18 15:15
文章分类
文章存档

2010年(17)

我的朋友
最近访客

分类: C/C++

2010-05-13 13:11:54

服务器端代码:


/*
* tcp-s.c
* Author: Alvin
* 基于TCP的服务器端代码
* 功能:服务器将客户端发来的内容倒序后发回到客户端
*
*/


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

#define MAXSIZE 20
#define MYPORT 6767
#define BACKLOG 10
 
char recvs[MAXSIZE];
char sends[MAXSIZE];
 
void str_ser(int sockfd)
{
    int n=0,i;
    while(1)
    {
        memset(recvs,0,sizeof(recvs));
        if((n=recv(sockfd,recvs,MAXSIZE,0))==0)
            return ;
        else
        {
            recvs[n]='\0';
            printf("TCP server receiced: %s\n",recvs);
 
            memset(sends,0,sizeof(sends));
            for (i=0;i<n;i++)
            sends[i]=recvs[n-1-i];
            sends[n]=0;
            sleep(1);

            send(sockfd,sends,n,0);
            printf("TCP server sent: %s\n",sends);
        }
    }
}
 
int main ( )
{

    int sockfd,new_fd,numbytes,ret;
    struct sockaddr_in my_addr;
    struct sockaddr_in their_addr;
    int sin_size,con_fd,pid;
    char *buf;

    /*创建监听套接口 */
    sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0)
    {
             printf("error in socket");
             exit(1);
    }
 
    my_addr.sin_family=AF_INET;
    my_addr.sin_port=htons(MYPORT);
    my_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    bzero(&(my_addr.sin_zero),8);
 
    /*绑定监听套接口 */
    ret=bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr));
    if(ret<0)
    {
        printf("error in binding\n");
        exit(1);
    }
 
    /* 监听连接请求*/
    ret=listen (sockfd,BACKLOG);
    if(ret<0)
    {
        printf("error in listening");
        exit(1);
    }
 
    while(1)
    {
        sin_size=sizeof(their_addr);
        con_fd=accept(sockfd,(struct sockaddr *)&their_addr,&sin_size);

        if(con_fd<0)
       {
            printf("error in accept");
            exit(1);
        }
        else
       {
           printf("TCP server and client connect successfully...\n");
           printf(" client IP:%s\n",(char *)inet_ntoa(their_addr.sin_addr));
       }
 
       /*创建新的连接 */
        if((pid=fork())==0)
        /*子进程代码 */
        {
            close(sockfd);
            str_ser(con_fd);
            /*接收并处理数据 */
            close(con_fd);
            exit(0);
        }
        else
       /*父进程代码 */
           close(con_fd);
    }
    close(sockfd);
    return 0;
}



客户端代码:

/*
* tcp-c.c
* Author: Alvin
* 基于TCP的客户端代码
* 功能:向服务器发送内容,并接收来自服务端的回复
*
*/

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
 
#define SERVER_PORT 6767
 
int main(int argc, char **argv)
{
    if (argc != 3)
    {
        printf("\nUsing: ./tcp-c IP string\n");
        printf("\tThe length of the string should not bigger than 20.\n\n");
        exit(0);
    }

    int socketfd,ret;
    struct sockaddr_in dest_addr;

    char pBuf2[20]={0};
    socketfd = socket(AF_INET, SOCK_STREAM, 0);

    if (socketfd == -1)
    {
        printf("TCP_client:socket create fialed.\r\n");
        return -1;
    }
         dest_addr.sin_family = AF_INET;
         dest_addr.sin_addr.s_addr = inet_addr(argv[1]);
         dest_addr.sin_port = htons(SERVER_PORT);
         bzero(&(dest_addr.sin_zero), 8);

         ret = connect(socketfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));

         if (ret < 0)
         {
                   printf("TCP_client:connect failed.\r\n");
                   return -1;
         }

         while (1)
         {
              ret = send(socketfd, argv[2], strlen(argv[2]), 0);
              printf("TCP_client send:%s\r\n",argv[2]);
              sleep(1);
              memset(pBuf2,0,sizeof(pBuf2));
              ret = recv(socketfd, pBuf2, sizeof(pBuf2), 0);
              if (ret>0)
              {
                   pBuf2[ret]=0;
                   printf("TCP_client recv:%s\r\n",pBuf2);
              }
         }
         return 0;

}


阅读(723) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~