#include <IOSTREAM>
#include "winsock2.h"
#include "string.h"
#define BUFFLEN 300
CRITICAL_SECTION gCriticalSection;
using namespace std;
int main()
{
WSADATA wsadata;
int nRC;
sockaddr_in srvAddr,clientAddr;
SOCKET clientsock;
char sendbuf[BUFFLEN],recvbuf[BUFFLEN];
nRC=WSAStartup(0x0101,&wsadata);
InitializeCriticalSection(&gCriticalSection);
if(nRC)
{
cout<<"Client initialize winsock error!"<<endl;
getchar();
return 0;
}
if(wsadata.wVersion!=0x0101)
{
cout<<"Client winsock version error!"<<endl;
WSACleanup();
getchar();
return 0;
}
cout<<"Client*s winsock initialized"<<endl;
clientsock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(clientsock==INVALID_SOCKET)
{
cout<<"Client creat socket error!"<<endl;
getchar();
WSACleanup();
return 0;
}
cout<<"Client socket create OK!"<<endl;
clientAddr.sin_family=AF_INET;
clientAddr.sin_port=htons(0);
clientAddr.sin_addr.S_un.S_addr=inet_addr("192.168.1.103");
nRC=bind(clientsock,(LPSOCKADDR)&clientAddr,sizeof(clientAddr));
if(nRC==SOCKET_ERROR)
{
cout<<"Client socket bind error!"<<endl;
printf("%d",WSAGetLastError());
closesocket(clientsock);
WSACleanup();
getchar();
return 0;
}
cout<<"Client socket bind OK!"<<endl;
srvAddr.sin_family=AF_INET;
srvAddr.sin_port=htons(21);
srvAddr.sin_addr.S_un.S_addr=inet_addr("192.168.1.103");
nRC=connect(clientsock,(LPSOCKADDR)&srvAddr,sizeof(srvAddr));
if(nRC==SOCKET_ERROR)
{
cout<<"Fail connect to server!"<<endl;
closesocket(clientsock);
WSACleanup();
getchar();
return 0;
}
cout<<"Success connect to server!"<<endl;
nRC=recv(clientsock,recvbuf,BUFFLEN,0);
if(nRC>0)
{
recvbuf[nRC]=*\0*;
printf("%s",recvbuf);
}
else if(nRC==SOCKET_ERROR)
{
cout<<"Transport fail!"<<endl;
closesocket(clientsock);
WSACleanup();
getchar();
return 0;
}
sprintf(sendbuf,"USER %s/r/n","zwd123");
nRC=send(clientsock,sendbuf,BUFFLEN,0);
if(nRC==SOCKET_ERROR)
printf("error\n");
printf("1");
nRC=recv(clientsock,recvbuf,BUFFLEN,0);
if(nRC>0)
{
recvbuf[nRC]=*\0*;
printf("%s",recvbuf);
}
else if(nRC==SOCKET_ERROR)
{
cout<<"Transport fail!"<<endl;
printf("%d",WSAGetLastError());
closesocket(clientsock);
WSACleanup();
getchar();
return 0;
}
recvbuf[nRC]=*\0*;
printf("%s",recvbuf);
getchar();
return 1;
}
#include "winsock2.h"
#include "string.h"
#define BUFFLEN 300
CRITICAL_SECTION gCriticalSection;
using namespace std;
int main()
{
WSADATA wsadata;
int nRC;
sockaddr_in srvAddr,clientAddr;
SOCKET clientsock;
char sendbuf[BUFFLEN],recvbuf[BUFFLEN];
nRC=WSAStartup(0x0101,&wsadata);
InitializeCriticalSection(&gCriticalSection);
if(nRC)
{
cout<<"Client initialize winsock error!"<<endl;
getchar();
return 0;
}
if(wsadata.wVersion!=0x0101)
{
cout<<"Client winsock version error!"<<endl;
WSACleanup();
getchar();
return 0;
}
cout<<"Client*s winsock initialized"<<endl;
clientsock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(clientsock==INVALID_SOCKET)
{
cout<<"Client creat socket error!"<<endl;
getchar();
WSACleanup();
return 0;
}
cout<<"Client socket create OK!"<<endl;
clientAddr.sin_family=AF_INET;
clientAddr.sin_port=htons(0);
clientAddr.sin_addr.S_un.S_addr=inet_addr("192.168.1.103");
nRC=bind(clientsock,(LPSOCKADDR)&clientAddr,sizeof(clientAddr));
if(nRC==SOCKET_ERROR)
{
cout<<"Client socket bind error!"<<endl;
printf("%d",WSAGetLastError());
closesocket(clientsock);
WSACleanup();
getchar();
return 0;
}
cout<<"Client socket bind OK!"<<endl;
srvAddr.sin_family=AF_INET;
srvAddr.sin_port=htons(21);
srvAddr.sin_addr.S_un.S_addr=inet_addr("192.168.1.103");
nRC=connect(clientsock,(LPSOCKADDR)&srvAddr,sizeof(srvAddr));
if(nRC==SOCKET_ERROR)
{
cout<<"Fail connect to server!"<<endl;
closesocket(clientsock);
WSACleanup();
getchar();
return 0;
}
cout<<"Success connect to server!"<<endl;
nRC=recv(clientsock,recvbuf,BUFFLEN,0);
if(nRC>0)
{
recvbuf[nRC]=*\0*;
printf("%s",recvbuf);
}
else if(nRC==SOCKET_ERROR)
{
cout<<"Transport fail!"<<endl;
closesocket(clientsock);
WSACleanup();
getchar();
return 0;
}
sprintf(sendbuf,"USER %s/r/n","zwd123");
nRC=send(clientsock,sendbuf,BUFFLEN,0);
if(nRC==SOCKET_ERROR)
printf("error\n");
printf("1");
nRC=recv(clientsock,recvbuf,BUFFLEN,0);
if(nRC>0)
{
recvbuf[nRC]=*\0*;
printf("%s",recvbuf);
}
else if(nRC==SOCKET_ERROR)
{
cout<<"Transport fail!"<<endl;
printf("%d",WSAGetLastError());
closesocket(clientsock);
WSACleanup();
getchar();
return 0;
}
recvbuf[nRC]=*\0*;
printf("%s",recvbuf);
getchar();
return 1;
}