예제 #1
0
파일: Server.cpp 프로젝트: quinsmpang/Tools
int main()
{
	// Create the main server socket.
	// It is used to listen for clients
	CServerSocket Server;
	if (!Server.Create(AF_INET, SOCK_STREAM))
	{
		cout << "Failed to create socket\n" ;
		return 0;
	}

	// Bind the IP address and port# to the main socket
	if (SOCKET_ERROR == Server.Bind("127.0.0.1", 3000))
	{
		cout << "Failed to bind IP address to socket\n" ;
		return 0;
	}

	// Listen on the socket for clients to connect
	if (SOCKET_ERROR == Server.Listen())
	{
		cout << "Listen on socket failed\n";
		return 0;
	}

	// Create the socket to communicate with the Client
	CServerSocket Client;
	cout << "Waiting for the client to connect\n";
	do
	{
		Server.Accept(Client, NULL, NULL);
	}
	while (SOCKET_ERROR == Client.GetSocket());

	cout << "Client connected\n";

	// Monitor the client socket for network events, such as data ready to receive
	Client.StartEvents();

	// Send data to the client
	cout << "Type data to send, type quit to exit\n";
	string s;
	for (;;)   // infinite loop
	{
		getline(cin, s);
		if (s == "quit") break;
		int i = Client.Send(s.c_str(), (int)s.length(), 0);
		cout << "Sending  " << i << " characters\n";
	}

	return 0;
}
예제 #2
0
void CServerConnect::ConnectToServer(CServer* server, bool multiconnect, bool bNoCrypt)
{
	if (!multiconnect) {
		StopConnectionTry();
		Disconnect();
	}
	connecting = true;
	singleconnecting = !multiconnect;
	theApp.emuledlg->ShowConnectionState();

	CServerSocket* newsocket = new CServerSocket(this, !multiconnect);
	m_lstOpenSockets.AddTail((void*&)newsocket);
	newsocket->Create(0, SOCK_STREAM, FD_READ | FD_WRITE | FD_CLOSE | FD_CONNECT, thePrefs.GetBindAddrA());
	newsocket->ConnectTo(server, bNoCrypt);
	connectionattemps.SetAt(GetTickCount(), newsocket);
}