Chat Room client program windows console application
The corresponding Linux server is implemented using the select model:
Chat Server implemented by the Select function in Linux
Http://blog.csdn.net/microtong/archive/2009/12/12/4989902.aspx
Run the command: client.exe 127.0.0.1 8000.
The command line parameters are respectively the IP address and port to be connected.
// Client. CPP <br/> # include <winsock2.h> <br/> # include <stdio. h> <br/> # include <windows. h> <br/> // introduce the static Link Library <br/> # pragma comment (Lib, "ws2_32.lib ") <br/> // buffer size <br/> # define buffer_size 4096 <br/> // command used to modify user name <br/> const char * Comment _name = "name"; <br/> // command used to exit the system <br/> const char * pai_bye = "bye "; <br/> // The thread responsible for receiving data <br/> DWORD winapi etethreadproc (lpvoid lpparameter); <br/> int main (INT argc, char * argv []) {<Br/> // check whether the IP address and port number are entered. <br/> If (argc! = 3) {<br/> printf ("Usage: % s IPaddress portnumber/N", argv [0]); <br/> exit (-1 ); <br/>}< br/> // convert the string's IP address to u_long <br/> unsigned long IP address; <br/> If (IP = inet_addr (argv [1]) = inaddr_none) {<br/> printf ("invalid IP Address: % s ", argv [1]); <br/> exit (-1); <br/>}< br/> // converts the port number to an integer <br/> short port; <br/> If (Port = atoi (argv [2]) = 0) {<br/> printf ("the port number is incorrect! "); <Br/> exit (-1); <br/>}< br/> printf (" connecting to % s: % d ...... /n ", inet_ntoa (* (in_addr *) & IP), Port); <br/> wsadata WSA; <br/> // initialize the socket DLL <br/> If (wsastartup (makeword (), & WSA )! = 0) {<br/> printf ("socket initialization failed! "); <Br/> exit (-1); <br/>}< br/> // create a socket <br/> socket sock; <br/> If (sock = socket (af_inet, sock_stream, ipproto_tcp) = invalid_socket) {<br/> printf ("failed to create socket! "); <Br/> exit (-1); <br/>}< br/> struct sockaddr_in serveraddress; <br/> memset (& serveraddress, 0, sizeof (sockaddr_in); <br/> serveraddress. sin_family = af_inet; <br/> serveraddress. sin_addr.s_un.s_addr = IP; <br/> serveraddress. sin_port = htons (port); <br/> // establish a connection with the server <br/> If (connect (sock, (sockaddr *) & serveraddress, sizeof (serveraddress )) = socket_error) {<br/> printf ("An error occurred while establishing the connection! "); <Br/> exit (-1); <br/>}< br/> // create a thread that receives data from the server <br/> DWORD threadid; <br/> createthread (null, 0, receivethreadproc, & sock, 0, & threadid); <br/> printf ("> "); <br/> char Buf [buffer_size]; <br/> while (1) {<br/> // read a row of data from the console <br/> gets (BUF ); <br/> printf (">"); </P> <p> // send it to the server <br/> If (send (sock, Buf, strlen (BUF ), 0) = socket_error) {<br/> printf ("failed to send data! "); <Br/> exit (-1 ); <br/>}</P> <p >}< br/> // clear the resources occupied by the socket <br/> wsacleanup (); <br/> return 0; <br/>}< br/> DWORD winapi etethreadproc (lpvoid lpparameter) {<br/> socket S = * (socket *) lpparameter; <br/> char receivebuf [buffer_size]; <br/> int bytes; <br/> while (1) {<br/> If (Bytes = Recv (S, receivebuf, sizeof (receivebuf), 0) = socket_error) {<br/> printf ("failed to receive data! /N "); <br/> exit (-1); <br/>}< br/> If (Bytes = 0) {<br/> printf ("server is down. /n "); <br/> exit (-1); <br/>}< br/> receivebuf [bytes] = '/0 '; <br/> // if the user inputs "bye" <br/> If (strcmp (receivebuf, pai_bye) = 0) {<br/> closesocket (s ); <br/> exit (0); <br/>}< br/> printf ("/n % s/n>", receivebuf ); <br/>}< br/>}