Hi folks, I started to learn a WinSock programming, so I want to realise one simple server-client application, both server and client will be on my own computer,
because I havn't conditions for different computers and ip addresses. This application simple should to do next staff :server send a some data to client and client should accept it and show that data in some control (edit).
This what I did in direction of server application:
Here is code for getting my ip address:Code:WSAStartup(version, &wsaData); sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); WSAAsyncSelect (sock, hwnd, WM_SOCKET_NOTIFY, FD_CONNECT | FD_READ | FD_WRITE)); addr.sin_family = AF_INET; addr.sin_port = htons("I don't know what to put here, maybe a 21 FTP ???"); addr.sin_addr.S_un.S_addr = inet_addr(hostip); connect(sock, (SOCKADDR*) &addr, sizeof(addr)); return TRUE;
Here are a declarations:Code:version = MAKEWORD(2, 0); WSAStartup(version, &wsaData); gethostname(hostname, 256); host = gethostbyname(hostname); strcpy(hostname, host->h_name); hostip = inet_ntoa(*((in_addr*)host->h_addr)); MessageBox(NULL, hostname, NULL, NULL); MessageBox(NULL, hostip, NULL, NULL); WSACleanup();
So first question, is everithing here ok, so did some line could be better done or is wrong etc, and what port to use in addr.sin_port ??Code:static WSADATA wsaData; static SOCKET sock; static SOCKADDR_IN addr; static char received; static unsigned long ulTime; static DWORD wEvent, wError; static HOSTENT *host; static char hostname; static char *hostip; static WORD version; #define WM_SOCKET_NOTIFY (WM_USER + 1)
Next if this code established a connection (with correct port), what function I must use to send a data to connected computer (example code should be excellent), and what functions must use a client for accepting a sent data (example code again should be great help). Did connection must be a from both side, I mean did client must be connected to a server computer for receiving data or he will just got message for pending data whos was sent to him and he just decide did or not to accept it.
Thanks again !