C语言编写的仿QQ局域网通讯程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// 系统添加网络连接的头文件引用及一些消息类型的宏定义:
#include
#include
#pragma comment(lib,"ws2_32.lib")
#define CLIENTSEND_EXIT 1
#define CLIENTSEND_TRAN 2
#define CLIENTSEND_LIST 3
#define SERVERSEND_SELFID 1
#define SERVERSEND_NEWUSR 2
#define SERVERSEND_SHOWMSG 3
#define SERVERSEND_ONLINE 4
FILE *ioutfileServer;
FILE *ioutfileClient;
struct CReceivePackage
{
int iType;
int iToID;
int IFromID;
char cBuffer[1024];
};
struct CSendPackage
{
int iType;
int iCurConn;
char cBuffer[1024];
};
struct CUserSocketInfo
{
int ID;
char cDstIP[64];
int iPort;
SOCKET sUserSocket;
};
struct CUser
{
int ID;
char cDstIP[64];
};
struct CUser usr[20];
int bSend=0;
int iMyself;
int iNew=0;
struct CUserSocketInfo usrinfo[20];
//退出函数
void ExitSystem()
{
if(ioutfileServer!=NULL)
fclose(ioutfileServer);
if(ioutfileClient!=NULL)
fclose(ioutfileClient);
WSACleanup();
exit(0);
}
//服务器端接收消息的线程
DWORD WINAPI threadproServer(LPVOID pParam)
{
SOCKET hsock=(SOCKET)pParam;
char cRecvBuffer[1024];
char cShowBuffer[1024];
int num=0;
if(hsock!=INVALID_SOCKET)
printf("start:\n");
while(1)
{
num=recv(hsock,cRecvBuffer,1024,0);
if(num>=0)
{
cRecvBuffer[num]='\0';
sprintf(cShowBuffer,"to me:%s\n",cRecvBuffer);
printf("%s",cShowBuffer);
fwrite(cShowBuffer,sizeof(char),strlen(cShowBuffer),ioutfileServer); fflush(ioutfileServer);
if(strcmp("exit",cRecvBuffer)==0)
{
ExitSystem();
}
}
}
return 0;
}
//检查IP
int CheckIP(char *cIP)
{
char IPAddress[128];
char IPNumber[4];
int iSubIP=0;
int iDot=0;
int iResult=0;
int iIPResult=1;
int i;
memset(IPNumber,0,4);
strncpy(IPAddress,cIP,128);
for(i=0;i<128;i++)
{
if(IPAddress[i]=='.')
{
iDot++;
iSubIP=0;
if(atoi(IPNumber)>255)
iIPResult=0;
memset(IPNumber,0,4);
}
else
{
IPNumber[iSubIP++]=IPAddress[i]; }
if(iDot==3 && iIPResult!=0)
iResult=1;
}
return iResult;
}
//创建点对点服务端
void CreateServer()
{
SOCKET m_SockServer;
struct sockaddr_in serveraddr;
struct sockaddr_in serveraddrfrom;
int iPort=4600;
int iBindResult=-1;
int iWhileCount=200;
struct hostent* localHost;
char* localIP;
SOCKET m_Server;
char cWelcomBuffer[]="Welcome to you";
int len=sizeof(struct sockaddr);
int iWhileListenCount=10;
DWORD nThreadId=0;
int ires;
char cSendBuffer[1024];
char cShowBuffer[1024];
ioutfileServer=fopen("MessageServer.txt","a");
m_SockServer=socket(AF_INET,SOCK_STREAM, 0);
printf("本机绑定的端口号(大于1024):");
scanf("%d",&iPort);
localHost=gethostbyname("");
localIP=inet_ntoa(*(struct in_addr *)*localHost->h_addr_list);
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(iPort);
serveraddr.sin_addr.S_un.S_addr=inet_addr(localIP);
iBindResult=bind(m_SockServer,(struct sockaddr*)&serveraddr,sizeof(struct sockaddr));
while(iBindResult!=0 && iWhileCount>0)
{
printf("绑定失败,重新输入:");
scanf("%d",iPort);
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(iPort);
serveraddr.sin_addr.S_un.S_addr=inet_addr(localIP);
iBindResult=bind(m_SockServer,(struct sockaddr*)&serveraddr,sizeof(struct