【tcp/ip】无连接交互式通信[udp]
server.cpp
#include <Winsock2.h>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#define DEFAULT_PORT 5060
#define BUFFER_LENGTH 1024
using namespace std;
string GetLocalIpAddress()
{
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(wVersionRequested, &wsaData) != 0)
return "";
char local[255] = {0};
gethostname(local, sizeof(local));
hostent* ph = gethostbyname(local);
if (ph == NULL)
return "";
in_addr addr;
memcpy(&addr, ph->h_addr_list[0], sizeof(in_addr)); // 这里仅获取第一个ip
string localIP;
localIP.assign(inet_ntoa(addr));
WSACleanup();
return localIP;
}
int main()
{
char date[100];
strcpy (date, const_cast<char*>(GetLocalIpAddress().c_str()));
int iPort = DEFAULT_PORT;
WSADATA wsaData;
SOCKET sSocket;
int iLen;
int iSend;
int iRecv;
char send_buf[BUFFER_LENGTH];
char recv_buf[BUFFER_LENGTH];
struct sockaddr_in ser,cli;
printf("-----------------------\n");
printf("Server waiting!\n");
printf("------------------------\n");
if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
{
printf("Failed to load Winsock.\n");
return 0;
}
sSocket = socket(AF_INET,SOCK_DGRAM,0);
if(sSocket == 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(sSocket,(LPSOCKADDR)&ser,sizeof(ser))==SOCKET_ERROR)
{
printf("bind()Failed:%d\n",WSAGetLastError());
return 0;
}
iLen = sizeof(cli);
while(1)
{
memset(recv_buf,0,sizeof(recv_buf));
iRecv=recvfrom(sSocket,recv_buf,BUFFER_LENGTH,0,(SOCKADDR*)&cli,&iLen);
if(iRecv==SOCKET_ERROR)
{
printf("recvfrom():%d\n",WSAGetLastError());
break;
}
else if(iRecv==0)break;
else
{
if(recv_buf[0]=='#') printf("client has already quit!\n");
else if(cli.sin_addr.s_addr==inet_addr(date))printf("Becky:%s\n",recv_buf);
else printf("Eric:%s\n",recv_buf);
// printf("Accepted client IP:[%s],port:[%d]\n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));
}
}
closesocket(sSocket);
WSACleanup();
return 0;
}
client.cpp
#include <Winsock2.h>
#include <stdio.h>
#include <string>
#define DEFAULT_PORT 5060
#define DATA_BUFFER 1024
using namespace std;
string GetLocalIpAddress()
{
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(wVersionRequested, &wsaData) != 0)
return "";
char local[255] = {0};
gethostname(local, sizeof(local));
hostent* ph = gethostbyname(local);
if (ph == NULL)
return "";
in_addr addr;
memcpy(&addr, ph->h_addr_list[0], sizeof(in_addr)); // 这里仅获取第一个ip
string localIP;
localIP.assign(inet_ntoa(addr));
WSACleanup();
return localIP;
}
int main(int argc,char *argv[])
{
char date[100];
strcpy (date, const_cast<char*>(GetLocalIpAddress().c_str()));
WSADATA wsaData;
SOCKET sClient,sSelf;
int iPort = DEFAULT_PORT;
int iLen,iSend,iRecv,iSend2,sLen;
char buf[DATA_BUFFER];
char recv_buf[DATA_BUFFER];
char send_buf[DATA_BUFFER]="Hello i am a cilent!";
struct sockaddr_in ser,ser2;
if(argc<2)
{
printf("Usage:client [server IP address]\n");
return 0;
}
memset(recv_buf,0,sizeof(recv_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]);
ser2.sin_family = AF_INET;
ser2.sin_port = htons(iPort);
ser2.sin_addr.s_addr=inet_addr(date);
sClient = socket(AF_INET,SOCK_DGRAM,0);
sSelf = socket(AF_INET,SOCK_DGRAM,0);
if(sClient==INVALID_SOCKET)
{
printf("socket()Failed:%d\n",WSAGetLastError());
return 0;
}
if(sSelf==INVALID_SOCKET)
{
printf("socket()Failed:%d\n",WSAGetLastError());
return 0;
}
sLen =sizeof(ser2);
iLen = sizeof(ser);
while(1)
{
gets(send_buf);
iSend=sendto(sClient,send_buf,sizeof(send_buf),0,(struct sockaddr*)&ser,iLen);
iSend2 = sendto(sSelf,send_buf,sizeof(send_buf),0,(struct sockaddr*)&ser2,sLen);
if(iSend==SOCKET_ERROR)
{
printf("sendto()Failed:%d\n",WSAGetLastError());
return 0;
}
else if(iSend==0) return 0;
if(iSend2==SOCKET_ERROR)
{
printf("sendto()Failed:%d\n",WSAGetLastError());
return 0;
}
else if(iSend2==0) return 0;
if(send_buf[0]=='#') break;
}
/*iRecv=recvfrom(sClient,recv_buf,sizeof(recv_buf),0,(struct sockaddr*)&ser,&iLen);
if(iRecv==SOCKET_ERROR)
{
printf("recvfrom()Failed:%d\n",WSAGetLastError());
return 0;
}
else if(iRecv==0) return 0;
else
{
printf("%s",recv_buf);
printf("recvfrom()succeeded!\n");
printf("-------------------\n");
}//*/
closesocket(sClient);
WSACleanup();
return 0;
}