[tcp/ip] 有链接交互式通信
client.cpp
#include <Winsock2.h>
#include <stdio.h>
#define DEFAULT_PORT 5050
#define DATA_BUFFER 1024
int main(int argc,char *argv[])
{
WSADATA wsaData;
SOCKET sClient;
int iPort = DEFAULT_PORT;
int iLen,iSend;
char buf[DATA_BUFFER];
struct sockaddr_in ser;
if(argc<2)
{
printf("Usage:client [server IP address]\n");
return 0;
}
memset(buf,0,sizeof(buf));
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Failed to load Winsock.\n");
return 0;
}
ser.sin_family = AF_INET;
ser.sin_port = htons(iPort);
ser.sin_addr.s_addr=inet_addr(argv[1]);
sClient = socket(AF_INET,SOCK_STREAM,0);
if(sClient==INVALID_SOCKET)
{
printf("socket()Failed:%d\n",WSAGetLastError());
return 0;
}
if(connect(sClient,(struct sockaddr*)&ser,sizeof(ser))==INVALID_SOCKET)
{
printf("connect()Failed:%d\n",WSAGetLastError());
return 0;
}
else
{
/*while(1)
{
iLen=recv(sClient,buf,sizeof(buf),0);
if(iLen==0)
{
return 0;
}
else if(iLen==SOCKET_ERROR)
{
printf("recv()Failed:%d\n",WSAGetLastError());
return 0;
}
if(buf[0]=='#') printf("break");
printf(":%s\n",buf);
}*/
while(1)
{
gets(buf);
iSend = send(sClient,buf,sizeof(buf),0);
if(iSend==SOCKET_ERROR)
{
printf("send()Failed:%d\n",WSAGetLastError());
break;
}
else if(iSend==0) break;
else
{
// printf("send()byte:%d\n",iSend);
// printf("-----------------------\n");
}
if(buf[0]=='#')break;
}
}
closesocket(sClient);
WSACleanup();
return 0;
}
server.cpp
#include<Winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#define DEFAULT_PORT 5050
int main()
{
while(1)
{
int iPort = DEFAULT_PORT;
WSADATA wsaData;
SOCKET sListen,sAccept;
int iLen;
int iSend;
char buf[100000];
struct sockaddr_in ser,cli;
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Failed to load Winsock.\n");
return 0;
}
sListen = socket(AF_INET,SOCK_STREAM,0);
if(sListen == INVALID_SOCKET)
{
printf("socket()Failed:%d\n",WSAGetLastError());
return 0;
}
ser.sin_family = AF_INET;
ser.sin_port = htons(iPort);
ser.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sListen,(LPSOCKADDR)&ser,sizeof(ser))==SOCKET_ERROR)
{
printf("bind()Failed:%d\n",WSAGetLastError());
return 0;
}
if(listen(sListen,5)==SOCKET_ERROR)
{
printf("listen()Failed:%d\n",WSAGetLastError());
return 0;
}
iLen = sizeof(cli);
sAccept = accept(sListen,(struct sockaddr*)&cli,&iLen);
if(sAccept==INVALID_SOCKET)
{
printf("accept()Failed:%d\n",WSAGetLastError());
return 0;
}
// printf("Accepted client IP:[%s],port:[%d]\n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));
/*while(1)
{
scanf("%s",buf);
iSend = send(sAccept,buf,sizeof(buf),0);
if(iSend==SOCKET_ERROR)
{
printf("send()Failed:%d\n",WSAGetLastError());
break;
}
else if(iSend==0) break;
else
{
// printf("send()byte:%d\n",iSend);
// printf("-----------------------\n");
}
if(buf[0]=='#')break;
}*/
while(1)
{
// cout<<"ee"<<endl;
iLen=recv(sAccept,buf,sizeof(buf),0);
if(iLen==0)
{
break;
}
else if(iLen==SOCKET_ERROR)
{
printf("recv()Failed:%d\n",WSAGetLastError());
break;
}
if(buf[0]=='#') printf("quit\n");
else {printf("Eric:");puts(buf);}
//cout<<"tt"<<endl;
}
closesocket(sAccept);
closesocket(sListen);
WSACleanup();
}
return 0;
}