#include <stdio.h>
#include <Winsock2.h>
#include <windows.h>
#include "resource.h"
#pragma comment(lib, "ws2_32.lib")
#define MAX_CONNECTED_NUM 1 //最大连接数,设置为1个
#define MAX_IPADDRESS 260
#define SOCKETMSG WM_USER+501
#define WINSOCK_VERION 0x0101 //MAKEWORD(2, 0)
char fname[MAX_PATH];
char bufIP[MAX_IPADDRESS];
char bufPORT[80];
char szDlgTitle[] = "聊天程序1.0版";
WSADATA ws;
SOCKET skt=INVALID_SOCKET;//服务器(或者是客户端)
//SOCKET clientSkt[MAX_CONNECTED_NUM];//客户端数组,暂定为5个
SOCKET connected_skt=INVALID_SOCKET;//服务器和客户端所连接上之后,所建立的SOCKET
//连接建立之后,所有的数据通信,都是通过这个 Connected_skt进行的
//这个新建的SOCKET不可以再接受客户端连接,只有原来的那一个可以接受
struct sockaddr_in addr;//服务器的socket地址
struct sockaddr_in clientaddr;//客户端的
void LogFile(char *p)
{
FILE *fp=fopen(fname,"a+");
fprintf(fp,"%s/n",p);
fclose(fp);
}
BOOL CreateServer(HWND hwnd)
{
if(WSAStartup(WINSOCK_VERION,&ws))
{
MessageBox(hwnd,"Winsock初始化失败", szDlgTitle,MB_OK|MB_ICONSTOP);
WSACleanup();
return FALSE;
}//初始化
if((skt=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
{
MessageBox(hwnd,"创建Socket失败!", szDlgTitle,MB_OK);
closesocket(skt);
return FALSE;
}
GetDlgItemText(hwnd,IDC_IP,bufIP,MAX_IPADDRESS);
GetDlgItemText(hwnd,IDC_PORT,bufPORT,79);
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY); //inet_addr("192.168.10.4");//
addr.sin_port=htons(atoi(bufPORT));
if( bind(skt,(SOCKADDR*)&addr,sizeof(addr)) == SOCKET_ERROR )
{
MessageBox(hwnd,"Socket绑定失败!", szDlgTitle,MB_OK);
return FALSE;
}
if( listen(skt,MAX_CONNECTED_NUM ) == SOCKET_ERROR )
{
MessageBox(hwnd,"监听失败!", szDlgTitle,MB_OK);
return FALSE;
}
char szIP[127],szPORT[127],buf[256];
GetDlgItemText(hwnd,IDC_IP,szIP,sizeof(szIP));
GetDlgItemText(hwnd,IDC_PORT,szPORT,sizeof(szPORT));
wsprintf(buf,">>>>>>服务器在地址:%s端口:%d监听",szIP,htons(atoi(bufPORT)));
SetDlgItemText(hwnd,IDC_REVTXT,buf);
//listen之后开始监听客户端的连接事件
if( WSAAsyncSelect(skt,hwnd,SOCKETMSG,FD_ACCEPT ) == SOCKET_ERROR )
{
MessageBox(hwnd,"WSAAsyncSelect() 失败", szDlgTitle,MB_OK);
return FALSE;
}
return TRUE;
}
BOOL CreateClient(HWND hwnd)
{
if(WSAStartup(WINSOCK_VERION,&ws)!=0)
{
MessageBox(hwnd,"Winsock初始化失败", szDlgTitle,MB_OK|MB_ICONSTOP);
WSACleanup();
return FALSE;
}//初始化
if((skt=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
{
MessageBox(hwnd,"创建Socket失败!", szDlgTitle,MB_OK);
closesocket(skt);
return FALSE;
}
GetDlgItemText(hwnd,IDC_IP,bufIP,MAX_IPADDRESS);
GetDlgItemText(hwnd,IDC_PORT,bufPORT,79);
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_addr=inet_addr(bufIP);
addr.sin_port=htons(atoi(bufPORT));
/*
if( bind(skt,(SOCKADDR*)&addr,sizeof(addr)) == SOCKET_ERROR )
{
MessageBox(hwnd,"Socket Bind 错误!", szDlgTitle,MB_OK);
return FALSE;
}
*/
int nConnect=connect(skt,(SOCKADDR*)&addr,sizeof(addr)); //请求连接
if(nConnect)
MessageBox(hwnd,"连接失败!",NULL,MB_OK);
else
MessageBox(hwnd,"连接成功!",NULL,MB_OK);
if( WSAAsyncSelect(skt,hwnd,SOCKETMSG,FD_READ|FD_CLOSE|FD_CONNECT ) == SOCKET_ERROR )
{
MessageBox(hwnd,"WSAAsyncSelect() 失败", szDlgTitle,MB_OK);
return FALSE;
}
return TRUE;
}
BOOL HttpClient(void)
{
WSADATA ws;
SOCKET s;
struct sockaddr_in addr;
int iResult;
long lResult;
char strSubAddr[100], strBuffer[100];
lResult = WSAStartup(0x0101,&ws);
s = socket(AF_INET,SOCK_STREAM,0); //建立的是一种TCP/IP 连接
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.s_addr = inet_addr("168.160.224.185"); // 计算机世界日报
iResult=connect(s,(struct sockaddr *)&addr, sizeof(addr));
if(SOCKET_ERROR == iResult)
{
// 连接失败
WSACleanup();
return FALSE;
}
else
{
// 连接成功
strcpy(strSubAddr, "GET /99/tips/ /r/n");
strcpy(fname, "C://index.htm");
iResult = send(s, strSubAddr,strlen(strSubAddr),0);
// 下载文件
do
{
strset(strBuffer,' ');
iResult = recv(s,strBuffer,sizeof(strBuffer),0);
LogFile(strBuffer);
} while( iResult !=0 );
}
WSACleanup();
return TRUE;
}