基于Linux的socket编程模板(3)
来源:未知 责任编辑:责任编辑 发表时间:2014-01-20 07:53 点击:次
/// @E-mail lujun.hust@gmail.com
/// @date 2011/08/21
//
//
// 修订说明:
//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "client.h"
// socket handle
int g_hSocket;
int connect_server( char *destIp, int destPort )
{
int result;
struct sockaddr_in address;
// create a socket
g_hSocket = socket(AF_INET,SOCK_STREAM,0);
// set server addr
address.sin_family = AF_INET;
// use server ip and listen port to connect
address.sin_addr.s_addr = inet_addr( destIp );
address.sin_port = htons(destPort);
// connect tcp server
result = connect(g_hSocket,(struct sockaddr *)&address,sizeof(address) );
if( result == -1 )
{
printf("[tcp client] can't connect server !\n");
return -1;
}
return 0;
}
int close_connect()
{
printf("close connect with server !\n ");
close(g_hSocket);
return 0;
}
int send_cmd( ServerPack sPack )
{
int recvBytes = 0;
int sendBytes = 0;
ReturnPack rPack;
// add cmd header
memcpy(sPack.cmdHeader,TCP_CMD_HEADER_STR,TCP_CMD_HEADER_LEN);
// send cmd
while(1)
{
sendBytes = send(g_hSocket,(uint8_t *)&sPack,SERVER_PACK_LEN,0);
if( sendBytes == SERVER_PACK_LEN )
相关新闻>>
- 发表评论
-
- 最新评论 更多>>