/*client 3.0 ADD connect wait time*/
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <time.h>
#pragma comment(lib,"ws2_32.lib") //windows socket2 system associate lib
#define PORT 3000
#define BUFF 128
main()
{
WSADATA wsa_dat; //windows socket2 system associate
char buf1[BUFF]={0}; //send buffer
char buf2[BUFF]; //recieve buffer
char buf_count[BUFF];
//char buf_null[BUFF];
char command[8]; //命令buffer
unsigned int socketfd=0; //server socket ID
struct sockaddr_in server;
char quit[]={"q!"}; //exit command
int count=0; //the most msgs send/recieve
int n_connect=0; //判断是否建立好了连接
char *ip;
char tmpbuf1[128];
char tmpbuf2[128];
fd_set rfds,wfds;
struct timeval tv,ct;
int retval=0;
char set_ip[16];
char host_name[128];
unsigned long ul = 1;
int error =-1;
int len=sizeof(int);
int ret=0;
int n_iotlsocket=0;
//windows socekt2 system associate
if(0!=WSAStartup(0x0101,&wsa_dat))
{
printf("winsock inint error\n");
exit(0);
}
gethostname(host_name,sizeof(host_name));
printf("host name: %s\n",host_name);
//设置服务器的IP地址
printf("set server IP address\n#");
scanf("%s",set_ip);
//printf("set wait time\n#");
//scanf("%d",wait_time);
//create socket return socket ID
socketfd = socket(PF_INET,SOCK_STREAM,0);
//给socket里面的元素赋0的初值
memset((char*) &server, sizeof(server), 0);
//写入目的地址
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = inet_addr(set_ip); //把IP转换成一个去掉点的无符号长整数
ip=inet_ntoa(server.sin_addr);
n_iotlsocket = ioctlsocket(socketfd,FIONBIO,(unsigned long*)&ul);
{
printf("n_iotlsocket = %d\n",n_iotlsocket);
}
ret = 0;
n_connect = connect(socketfd, (struct sockaddr*)&server,sizeof(server));
{
printf("n_connect = %d\n",n_connect);
}
printf("connecting...");
if(n_connect==-1)
{
ct.tv_sec=2;
ct.tv_usec=0;
FD_ZERO(&wfds);
FD_SET(socketfd,&wfds);
if(retval=select(0,NULL,&wfds,NULL,&ct)>0)
{
getsockopt(socketfd+1, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
if(error==0)
{
ret =1;
}else ret =0;
}else ret =0;
}else ret =1;
ul=0;
ioctlsocket(socketfd,FIONBIO,(unsigned long*)&ul);
if(!ret)
{
closesocket(socketfd);
printf("\nconnect error!\n");
}
else{
printf("\nn_connect = %d",n_connect);
printf("\nconnect succeed!\n");
ip = inet_ntoa(server.sin_addr);
printf("server IP Address: %s\n",ip);
printf("server Port: %d\n",PORT);
for(count = 0;count<8888;count++)
{
printf("#");
scanf("%s",command);
if(strcmp(command,quit)==0)
{
break;
}
switch(*command)
{
case 's':
printf("smsg#");
scanf("%s",buf_count);
if(strlen(buf_count)>BUFF)
{
printf("You message too long,please cut it.\n");
continue;
}
strcpy(buf1,buf_count);
send(socketfd,buf1,sizeof(buf1),0);
_strtime(tmpbuf1);
printf("%s smsg@\n%s\n",tmpbuf1,buf1);
continue;
case 'r':
tv.tv_sec=1;
tv.tv_usec=0;
FD_ZERO(&rfds);
FD_SET(socketfd,&rfds);
if(retval=select(10,&rfds,NULL,NULL,&tv))
{
recv(socketfd,buf2,sizeof(buf2),0);
_strtime(tmpbuf2);
printf("%s rmsg@\n%s\n",tmpbuf2,buf2);
//strcpy(buf2,buf_null);
}else printf("U have no message.\n");
continue;
case 'h':
printf(" Help\n");
printf("s send message.\n");
printf("r receive message.\n");
printf("q! quit program.\n");
continue;
default:
printf("%s :No such command.\n",command);
continue;
}
}
printf("\bconnect closed.\n");
}
closesocket(socketfd);
WSACleanup();
printf("socket closed.");
return 0;
}