Chinaunix首页 | 论坛 | 博客
  • 博客访问: 158921
  • 博文数量: 56
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 502
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-18 14:21
文章分类

全部博文(56)

文章存档

2010年(39)

2009年(17)

我的朋友

分类: LINUX

2009-12-20 18:10:45

#ifndef MY_CTCPCLIENT
#define MY_CTCPCLIENT
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

class CTcpClient
{
  public:
    CTcpClient();
    int connectServer(const char* hostIP, unsigned short hostPort);
    int connectServer(unsigned int hostIP, unsigned short hostPort);
    int sendData(void *data, unsigned int length);
    int recvData(void *data, unsigned int length);
    int closeClient();
  private:
       int sock_fd;
     struct sockaddr_in local_addr;
    struct sockaddr_in dest_addr;
};
#endif


#include "CTcpClient.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <arpa/inet.h>

CTcpClient::CTcpClient()
{
    memset(&local_addr,0,sizeof(local_addr));
    memset(&dest_addr, 0, sizeof(dest_addr));
    sock_fd = -1;
}

int CTcpClient::connectServer(const char* serverIP, unsigned short serverPort)
{
     dest_addr.sin_family = AF_INET; /* 协议名 */
   dest_addr.sin_addr.s_addr = inet_addr(serverIP); /* 自动分配地址 */
   dest_addr.sin_port = htons(serverPort); /* 端口号 */
   if(sock_fd!=-1)
       close(sock_fd);
   if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
   {
        perror(strerror(errno));
        return -1;
   }
   if(connect(sock_fd, (struct sockaddr*)&dest_addr, sizeof(struct sockaddr)) < 0)
   {
        perror(strerror(errno));
        return -1;
   }
   return 1;
}

int CTcpClient::connectServer(unsigned int serverIP, unsigned short serverPort)
{
     dest_addr.sin_family = AF_INET; /* 协议名 */
   dest_addr.sin_addr.s_addr =htonl(serverIP); /* 自动分配地址 */
   dest_addr.sin_port = htons(serverPort); /* 端口号 */
   if(sock_fd!=-1)
       close(sock_fd);
   if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
   {
        perror(strerror(errno));
        return -1;
   }
   if(connect(sock_fd, (struct sockaddr*)&dest_addr, sizeof(struct sockaddr)) < 0)
   {
        perror(strerror(errno));
        return -1;
   }
     return 1;
}

int CTcpClient::sendData(void *data, unsigned int length)
{
    int send_len = 0;
    if((send_len = send(sock_fd, data, length, 0)) < 0 )
    {
         perror(strerror(errno));
         return -1;
    }
    else
    {
        return send_len;
    }
}

int CTcpClient::recvData(void *data, unsigned int length)
{//接收数据即返回
    int rev_len = 0;
    if((rev_len = recv(sock_fd, data, length, 0)) < 0)
    {
         perror(strerror(errno));
         return -1;
    }
    else
        return rev_len;
}

int CTcpClient::closeClient()
{
    if(sock_fd!=-1)
  {     
       close(sock_fd);
       sock_fd = -1;
  }
  return 1;
}

#ifdef DEBUG
int main()
{
    unsigned char buffer[1024]={'\n'};
    CTcpClient myclient;
    myclient.connectServer("192.168.1.198",22082);
    while(1)
    {
        myclient.sendData((void*)"hello server",13);
     myclient.recvData(buffer,1024);
     printf("client:%s\n",buffer);
     sleep(2);
    }

}
#endif

g++ -o tcp_c CTcpClient.cpp -Wall -DDEBUG
阅读(551) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~