winsock select模型客户端

// client.cpp : 定义控制台应用程序的入口点。
//




#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers
#include <winsock2.h>
#include <WinSock.h>
#include <Ws2tcpip.h>
#include <stdio.h>




// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
#include <mstcpip.h>




#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <windows.h>




#define CONECTADO 1
#define DESCONECTADO 2
#define SOCKET_CONNECT_STATUS_ERROR 1000
#define HTONS(a) (((0xFF&a)<<8) + ((0xFF00&a)>>8))
#define CELL_SIZE  255
#define  MAX_MESSAGE   1024
SOCKET client;
 char* client_write(int fd, char *buffer, int len);








//域名转ip
ULONG  Host2Ip( char * host )
{
struct hostent * phost;
ULONG ret;
phost = gethostbyname(host);
if (phost)
{
ret = *(ULONG*)(phost->h_addr);
}else
{
ret = INADDR_NONE;
}
return ret;
}








//客户单连接服务器
 int  client_connect( char *host, int port )
{
WORD wVersionRequested; 
WSADATA wsaData; 
int err; 
wVersionRequested = MAKEWORD(1, 1); 




err = WSAStartup(wVersionRequested, &wsaData); 




if (err != 0)  return err; 




if ( LOBYTE( wsaData.wVersion ) != 1 ||  HIBYTE( wsaData.wVersion ) != 1 )

WSACleanup(); 
return __LINE__; 
}




#if USE_DOMAIN
struct sockaddr_in addr;
BOOL bOptVal = TRUE;
int bOptLen = sizeof(BOOL);




ULONG ip;
ULONG sock = INVALID_SOCKET;




ip = Host2Ip(host);




if (ip != INADDR_NONE)
{
addr.sin_addr.S_un.S_addr = ip;
addr.sin_port = HTONS(port);
if (addr.sin_addr.S_un.S_addr != INADDR_NONE)
{
addr.sin_family = AF_INET;
client = (ULONG)socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if ( client != INVALID_SOCKET )
{
int conn_status = connect(client, (struct sockaddr *)&addr, sizeof(addr));
if ( conn_status == SOCKET_ERROR )
{
closesocket(client);
sock = INVALID_SOCKET;
}
}
}
}
#else
client = (ULONG)socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in serverAddr;
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons( 60000);
serverAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
ULONG nonblock = 1;








if(connect(client ,(sockaddr*)&serverAddr,sizeof(serverAddr)) == -1)
{
int ret = WSAGetLastError();


// return 1;
}












//if (ioctlsocket(client, FIONBIO, &nonblock) == SOCKET_ERROR)
//{
// printf("ioctlsocket(FIONBIO) failed with error %d\n", WSAGetLastError());
// return 1;
//}
//else
// printf("ioctlsocket(FIONBIO) is OK!\n");












#endif




if ( client == INVALID_SOCKET )
{
closesocket( client );
}
  return( WSAGetLastError() );
}




//客户端关闭连接
 int  client_disconnect()
{
closesocket(client);
return 1;
}
















//客户端发送数据
 char* client_write(int fd, char *buffer, int len)
{
// 阻塞数据接收的问题 怎么解决 还是需要用select模型的去解决问题
int ret = send( fd , buffer , len , 0);
if(ret == -1)
{
printf("tcp 发送数据断开");
}
return NULL;
}




 //
 




//客户端接收数据
 char* client_recv(int fd,int sec, int msec)
{
// 阻塞数据接收的问题 怎么解决 还是需要用select模型的去解决问题
timeval outTime;
outTime.tv_sec = sec;   //设置等待时间为1s
outTime.tv_usec = msec; //毫秒




fd_set fdread;




FD_ZERO(&fdread);
FD_SET(fd, &fdread);//sessionSock为之前创建的会话套接字
select(0, &fdread, NULL, NULL, &outTime);
if(FD_ISSET(fd, &fdread))//判断套接字是否还在集合中
{




static char clientbuffer[1024];
memset(clientbuffer, 0x00,1024);
int ret = recv( fd , clientbuffer , 1024 , 0);
}




return NULL;
}








 void smtpIp(char * url)
 {
HOSTENT * iphost;
char ipconfig[20];
iphost =gethostbyname("www.baidu.com");
if(iphost!= NULL)
{
int i = 0;




while(iphost->h_addr_list[i])
{
char ip[20];
memcpy(&ip, inet_ntoa(*((struct in_addr *)iphost->h_addr_list[i])), 20);
printf("%s", ip);




memset(ip, '\0', 20);
i++;
}
}
int b = 0;
 }




int _tmain(int argc, _TCHAR* argv[])
{
client_connect("www.baidu.com",8000);




while (true)
{
if (client == -1)
{
client_connect("www.baidu.com",8000);
}else
{
client_write(client, "fuck",strlen("fuck"));
client_recv(client, 0, 20);
}
Sleep(20);




}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值