Trao đổi với tôi

www.hdphim.info OR www.chepphimhdtv.com

12/16/09

[Programming] Giao tiếp đơn giản giữa client và server bằng WinSock

Author: T4mQu0c

Source đơn giản còn nhiều bug
Client:
Code:
#include 
#include
#include
#include

#pragma comment(lib, "ws2_32.lib")
#define BUFF_MAX 1024

BOOL ConnectServer(char *ip, unsigned int port)
{
// Khai bao bien
WSADATA wsaData;
SOCKADDR_IN connect_sockaddr;
SOCKET connect_socket;

printf("[+] Client ... \n");
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
{
printf("Failed: %d)\n", GetLastError());
return FALSE;
}

connect_socket = WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,NULL,NULL);
if (connect_socket == INVALID_SOCKET)
{
printf("Failed: %d)\n", GetLastError());
WSACleanup();
return FALSE;
}

// Set IP, port
connect_sockaddr.sin_port = htons(port);
connect_sockaddr.sin_family = AF_INET;
connect_sockaddr.sin_addr.s_addr = inet_addr(ip);

printf("[+] Connect to %s:%d...\n", ip, port);
if (WSAConnect(connect_socket, (sockaddr*) &connect_sockaddr, sizeof(sockaddr), NULL, NULL, NULL, NULL) == SOCKET_ERROR)
{
printf("Failed: %d)\n", GetLastError());
WSACleanup();
return EXIT_FAILURE;
}

// Nhan ket qua tu server (dong Welcome tu Server)
char buff[BUFF_MAX] = {0};
recv(connect_socket, buff, BUFF_MAX, 0);
printf("%s\n", buff);

while(TRUE)
{
ZeroMemory(buff, sizeof(buff));
printf("Enter command:");
scanf("%s", &buff);
if (stricmp(buff, "quit") == 0)
goto CleanUp;

send(connect_socket, buff, strlen(buff), 0);
}

CleanUp:
closesocket(connect_socket);
WSACleanup();
return TRUE;
}

int _tmain(int argc, char* argv[])
{
if (argc != 3)
{
printf("Invalid parameter\n");
printf("%s \n", argv[0]);
return 0;
}

char ip[16] = {0};
int port;
strcpy(ip, argv[1]);
port = atoi(argv[2]);

// Kiem tra Port
if (port <> 65535)
{
printf("Invalid Port\n");
return 0;
}

ConnectServer(ip, port);

return 0;
}
Server:
Code:
#include 
#include
#include
#include

#pragma comment(lib, "ws2_32.lib")
#define BUFF_MAX 1024

BOOL ListenPort(unsigned int port)
{
// Khai bao bien
WSADATA wsaData;
SOCKET Winsock;
SOCKET ListenSock;
sockaddr_in Winsock_In;

printf("[+] Server ... \n");
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
{
printf("Failed: %d)\n", GetLastError());
return FALSE;
}

Winsock = WSASocket(AF_INET,SOCK_STREAM,IPPROTO_TCP,NULL,NULL,NULL);
if (Winsock == INVALID_SOCKET)
{
printf("Failed: %d)\n", GetLastError());
WSACleanup();
return FALSE;
}

// Set IP, port
Winsock_In.sin_family = AF_INET;
Winsock_In.sin_port = htons(port);
Winsock_In.sin_addr.s_addr = htonl(INADDR_ANY);

// Bind Port
if(bind(Winsock,(SOCKADDR*)&Winsock_In,sizeof(sockaddr_in)) == SOCKET_ERROR)
{
printf("Failed: %d)\n", GetLastError());
WSACleanup();
return FALSE;
}

// Listen Port
printf("[+] Listen port: %d\n", port);
if(listen(Winsock,5) == SOCKET_ERROR)
{
printf("Failed: %d)\n", GetLastError());
WSACleanup();
return FALSE;
}

printf("[+] Wait for Connect on port : %d\n", port);
if((ListenSock=accept(Winsock,NULL,NULL))==INVALID_SOCKET)
{
printf("Failed: %d)\n", GetLastError());
WSACleanup();
return FALSE;
}

printf("[+] Client connectd\n");

// Send welcome to Client
char szWelcome[] = "Xin chao ban";
send(ListenSock, szWelcome, strlen(szWelcome),0 );

char buff[BUFF_MAX] = {0};
while(TRUE)
{
ZeroMemory(buff, sizeof(buff));
recv(ListenSock, buff, BUFF_MAX, 0);
if (stricmp(buff, "bye") == 0) // Thoat
break;
else
system(buff); // Thuc hien lenh cua client tren Server
}

CleanUp:

closesocket(ListenSock);
closesocket(Winsock);
WSACleanup();

return TRUE;
}

int _tmain(int argc, _TCHAR* argv[])
{
ListenPort(12345);
return 0;
}
- Trước tiên chạy server trước để listen port 12345
- Sau đó chạy client với tham số
ví dụ:
Client 127.0.0.1 12345

No comments:

Post a Comment