【tcp/ip】UDP群聊系统
server
#include <Winsock2.h>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <bits/stdc++.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;
}
struct user
{
int port;
char ip[100];
SOCKET sc;
struct sockaddr_in ser;
int isend,ilen;
user(int _port,char* _ip)
{
port=5060;
strncpy(ip, _ip, sizeof(ip));
ser.sin_family = AF_INET;
ser.sin_port = htons(port);
ser.sin_addr.s_addr=inet_addr(ip);
sc = socket(AF_INET,SOCK_DGRAM,0);
if(sc==INVALID_SOCKET)
{
printf("socket()Failed:%d\n",WSAGetLastError());
}
ilen = sizeof(ser);
}
};
vector<user>u;
int vis[1000001];
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(vis,0,sizeof(vis));
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
{
user v(ntohs(cli.sin_port),inet_ntoa(cli.sin_addr));
printf("Accepted client IP:[%s],port:[%d]\n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));
int flag=0;
for(int j=0;j<u.size();j++)
{
//cout<<strcmp(u[j].ip,v.ip)<<endl;
if(strcmp(u[j].ip,v.ip)==0)flag=1;
}
if(!flag) u.push_back(v);
if(recv_buf[0]=='#')
{
printf("client has already quit!\n");
vector<user>::iterator itor;
for(itor=u.begin();itor!=u.end();itor++)
{
if((*itor).port==v.port) itor = u.erase(itor);
}
}
else
{
for(int i=0;i<u.size();i++)
{
u[i].isend=sendto(u[i].sc,recv_buf,sizeof(recv_buf),0,(struct sockaddr*)&u[i].ser,u[i].ilen);
if(u[i].isend==SOCKET_ERROR)
{
printf("sendto()Failed:%d\n",WSAGetLastError());
return 0;
}
}
printf("%s\n",recv_buf);
}
// else if(cli.sin_addr.s_addr==inet_addr(date))printf("Íõ³¿ÄÝ:%s\n",recv_buf);
// else printf("Ê·ÖÐÎä:%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;
}
send
#include <Winsock2.h>
#include <stdio.h>
#include <string>
#define DEFAULT_PORT 5060
#define DATA_BUFFER 1024
using namespace std;
int main(int argc,char *argv[])
{
WSADATA wsaData;
SOCKET sClient,sSelf;
int iPort = DEFAULT_PORT;
int iLen,iSend,iRecv;
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]);
sClient = socket(AF_INET,SOCK_DGRAM,0);
if(sClient==INVALID_SOCKET)
{
printf("socket()Failed:%d\n",WSAGetLastError());
return 0;
}
iLen = sizeof(ser);
while(1)
{
gets(send_buf);
iSend=sendto(sClient,send_buf,sizeof(send_buf),0,(struct sockaddr*)&ser,iLen);
if(iSend==SOCKET_ERROR)
{
printf("sendto()Failed:%d\n",WSAGetLastError());
return 0;
}
else if(iSend==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;
}
#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 printf("ȺÏûÏ¢£º%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;
}