Exemplo n.º 1
0
void CEditServer::OnGetcgi() 
{
	CSocket socket;
	CArchive*in;
	CArchive*out;
	CSocketFile* pFile;

	if (!socket.Create())
	{
		return;
	}

	while (!socket.Connect(GETCGI_HOST,GETCGI_PORT))
	{
		return;
	}
	pFile = new CSocketFile(&socket);
	in = new CArchive(pFile,CArchive::load);
	out = new CArchive(pFile,CArchive::store);

	CString str= "GET ";
	str += GETCGI_PATH;
	str += " HTTP/1.0\n\n";

	out->Write(str, str.GetLength());
    out->Flush();

	CString line;
	int count=0;
	CString n,h,p;
	while (in->ReadString(line)) {
		int c=line.Find("####");
		if (c==-1)
			continue;
		n=line.Left(c);
		line=line.Mid(c+4);
		c=line.Find("####");
		if (c==-1)
			continue;
		h=line.Left(c);
		p=line.Mid(c+4);
		m_name[count]=n;
		m_host[count]=h;
		m_port[count]=atoi(p);
		count++;
	}
	if (count!=0)
		m_count=count;
	in->Close();
	out->Close();
	pFile->Close();
	socket.Close();

	delete out;
	delete in;
	delete pFile;
	UpdateList();
	m_list.SetCurSel(0);
	return;
}
BOOL TestConnection( LPCTSTR lpstrAddress, int nPort)
{
	CSocket* pSocket;

	pSocket = new CSocket;
	ASSERT(pSocket);

	if (!pSocket->Create())
	{
		delete pSocket;
		pSocket = NULL;
		return FALSE;
	}

	while (!pSocket->Connect( lpstrAddress, nPort))
	{
		delete pSocket;
		pSocket = NULL;
		return FALSE;
	}

	pSocket->Close();
	delete pSocket;
	return TRUE;
}
void CTcpClientDlg::OnGetSource() 
{
	 CSocket s;
	 
	 if(!s.Create()) {
		 AfxMessageBox("소켓 생성 실패");
		 return;
	 }

	 if(!s.Connect("www.google.com", 80)) {
		 AfxMessageBox("접속 실패!");
		 return ;
	 }

	 //GET 명령으로 데이터를 얻어옴
	 //GET 명령 형식 : GET 대상URL HTTP/1.0 <enter><enter>
	 char * cmd = "GET / HTTP/1.0\r\n\r\n";

	 s.Send(cmd, strlen(cmd));

	 char buf[100];
	 ZeroMemory(buf, 100);
	 while(s.Receive(buf, 1000)) {
		 AfxMessageBox(buf);
		 ZeroMemory(buf, 1000);
	 }
}
void CNetworkTalkClientDlg::OnBtnSend() 
{
	// TODO: Add your control notification handler code here
	CSocket sockClient;
	sockClient.Create();
	sockClient.Connect(m_serverIPAddress,8000);
	g_serverIPAddress=m_serverIPAddress;
	sockClient.Send(m_sendText,m_sendText.GetLength());
	sockClient.Close();
	
}
Exemplo n.º 5
0
void main()
{
	AfxSocketInit();
	CSocket sock;
	int n = sock.Create(PORT1);
	if(!n)
	{
		cout << "创建句柄失败:" << GetLastError() << endl;
		return;
	}
	sock.Listen();

	while (true)
	{
		
		CSocket socka;
		n = sock.Accept(socka);
		if(!n)
		{
			cout << "接受对方连接失败:" << GetLastError() << endl;
			return;
		}
		while (true)
		{
			char s[256];
			n = socka.Receive(s,sizeof(s));
			if(SOCKET_ERROR == n)
			{
				cout << "接收数据失败:" << GetLastError() << endl;
				break;
			}
			if(!n)
			{
				cout << "结束接收数据,句柄关闭!" << endl;
				break;
			}
			cout << s <<endl;
		}
		socka.Close();

	}
}