Exemplo n.º 1
0
int main(void)
{
	BOOL bRet;
	HANDLE hRet;

	wsa_init();

	iocp = w_iocp_create();
	DIE(iocp == NULL, "w_iocp_create");

	/* Create server socket. */
	listenfd = tcp_create_listener(ECHO_LISTEN_PORT, DEFAULT_LISTEN_BACKLOG);
	DIE(listenfd == INVALID_SOCKET, "tcp_create_listener");

	hRet = w_iocp_add_handle(iocp, (HANDLE) listenfd);
	DIE(hRet != iocp, "w_iocp_add_handle");

	/* Use AcceptEx to schedule new connection acceptance. */
	create_iocp_accept();

	dlog(LOG_INFO, "Server waiting for connections on port %d\n", ECHO_LISTEN_PORT);

	/* server main loop */
	while (1) {
		OVERLAPPED *ovp;
		ULONG_PTR key;
		DWORD bytes;

		/* Wait for overlapped I/O. */
		bRet = w_iocp_wait(iocp, &bytes, &key, &ovp);
		if (bRet == FALSE) {
			DWORD err;

			err = GetLastError();
			if (err == ERROR_NETNAME_DELETED) {
				connection_remove((struct connection *) key);
				continue;
			}
			DIE(bRet == FALSE, "w_iocp_wait");
		}

		/*
		 * Switch I/O notification types. Consider
		 *   - new connection requests (on server socket);
		 *   - socket communication (on connection sockets).
		 */

		if (key == listenfd) {
			dlog(LOG_DEBUG, "New connection\n");
			handle_new_connection(ovp);
		}
		else {
			handle_aio((struct connection *) key, bytes, ovp);
		}
	}

	wsa_cleanup();

	return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
	//================De aici===============================
	static struct connection *connn;
	char buffer[256];

	wsa_init();//ai nevoie de functia asta in main
	
	connectfd = tcp_connect_to_server(argv[1], atoi(argv[2]));
	DIE(connectfd == INVALID_SOCKET, "tcp_connect_to_server");

	
	/* Instantiate new connection handler. */
	connn = connection_create(connectfd);

	//Aici in primul receive primesti datele gen pozitii initiale
	memcpy(connn->send_buffer,"-1.000000 -1.000000 -324.042542 -0.000003 -305.136108 303.976868 158.605179 -160.047546",75);
	connn->bytes_sent = 3;
	printf("Sent : %s\n",&connn->send_buffer);
	connection_schedule_socket_send(connn);
	Sleep(100);
	connection_schedule_socket_receive(connn);
	printf("buffer : %s\n",connn->recv_buffer);
	//=========================Pana aici===========================
	//=====================trebuie pus in mainul tau======================
	/* client main loop ;acest loop poate fi pus intr-o functie daca vrei*/
	while (1) {
		
		//Astepti comanda de la tastatura
		printf("Please enter the command (or 'quit' to exit): \n");
    	memset(buffer, 0 , 256);
    	fgets(buffer, 255, stdin);

		if (buffer[0]=='q'){
			tcp_close_connection(connn->sockfd);	
			exit(0);
		}

		//Copiezi comanda pentru a o trimite la server
		memcpy(connn->send_buffer,buffer,256);
		connn->bytes_sent = 256;
		printf("Sent : %s\n",&connn->send_buffer);
		connection_schedule_socket_send(connn);
		Sleep(100);
		connection_schedule_socket_receive(connn);
		//desi nu e nevoie acum in buffer e tot ce ai primit de la server
		memcpy(buffer,connn->recv_buffer,256);
		//==========================================
		//Aici poti s afolosesti campul connn->recv_buffer sau buffer pentru a procesa datele tale;eu doar le afisez
		//============================================
		printf("Received: %s\n",&connn->recv_buffer);
		
		
	}

	wsa_cleanup();//ai nevoie de functia asta in main

	return 0;
}
int main(void)
{
	SOCKET listenfd;	/* server socket */
	SOCKET connectfd;	/* client communication socket */

	wsa_init();

	/* create server socket */
	listenfd = tcp_create_listener(SIMPLE_WEB_LISTEN_PORT, DEFAULT_LISTEN_BACKLOG);
	DIE(listenfd < 0, "tcp_create_listener");

	connectfd = accept_connection(listenfd);
	receive_request(connectfd);
	send_reply(connectfd);

	closesocket(listenfd);
	wsa_cleanup();

	return 0;
}