Client :
- Create socket (socket)
- Connecting to a server (connect)
- Communication (SEND,RECV or Write,read)
- Close socket (closesocket)
Example code:
intMainintargcChar*argv[]) { Const intBuf_size = -; Wsadata WSD; //wsadata VariableSOCKET Shost;//Server SocketsSockaddr_in servaddr;//Server Address CharBuf[buf_size];//Receive data buffers CharBufrecv[buf_size]; intRetVal;//return value//initializing a nested-word dynamic library if(WSAStartup (Makeword (2,2), &WSD)! =0) {cout<<"WSAStartup failed!"<<Endl; return-1; } //Creating SocketsShost =sockets (Af_inet, Sock_stream, ipproto_tcp); if(Invalid_socket = =shost) {cout<<"Socket failed!"<<Endl; WSACleanup ();//releasing a socket resource return-1; } //Set server addressservaddr.sin_family =af_inet; ServAddr.sin_addr.s_addr= Inet_addr ("127.0.0.1"); Servaddr.sin_port= Htons (( Short)4999); intNservaddlen =sizeof(SERVADDR); //connecting to a serverRetVal = Connect (Shost, (LPSOCKADDR) &servaddr,sizeof(SERVADDR)); if(Socket_error = =retVal) {cout<<"Connect failed!"<<Endl; Closesocket (Shost); //Close SocketWSACleanup ();//releasing a socket resource return-1; } while(true){ //sending data to the serverZeroMemory (buf, buf_size); cout<<"to send data to the server:"; CIN>>buf; RetVal= Send (Shost, buf, strlen (BUF),0); if(Socket_error = =retVal) {cout<<"Send failed!"<<Endl; Closesocket (Shost); //Close SocketWSACleanup ();//releasing a socket resource return-1; } //recvline (Shost, bufrecv);Recv (Shost, BUFRECV,5,0);//Receive server-side data and receive only 5 characterscout << Endl <<"To receive data from the server:"<<Bufrecv; } //ExitClosesocket (Shost);//Close SocketWSACleanup ();//releasing a socket resource return 0;}
Service side:
- Create socket (socket)
- Bind socket (BIND)
- Turn on monitoring (listen)
- Acceptance Request (Accept)
- Communication (SEND,RECV or Write,read)
- Close socket (closesocket)
Example code:
int_tmain (intargcChar*argv[]) { Const intBuf_size = -; Wsadata WSD; //wsadata VariableSOCKET sserver;//Server SocketsSOCKET sclient;//Client SocketsSockaddr_in Addrserv;//Server Address CharBuf[buf_size];//Receive data buffers CharSendbuf[buf_size];//data returned to the client intRetVal;//return value//initializing a nested-word dynamic library if(WSAStartup (Makeword (2,2), &WSD)! =0) {cout<<"WSAStartup failed!"<<Endl; return 1; } //Creating SocketsSserver =sockets (Af_inet, Sock_stream, ipproto_tcp); if(Invalid_socket = =sserver) {cout<<"Socket failed!"<<Endl; WSACleanup ();//release socket resources; return-1; } //Server socket Addressaddrserv.sin_family =af_inet; Addrserv.sin_port= Htons (4999); AddrServ.sin_addr.s_addr=Inaddr_any; //binding SocketsRetVal = Bind (Sserver, (LPSOCKADDR) &addrserv,sizeof(sockaddr_in)); if(Socket_error = =retVal) {cout<<"bind failed!"<<Endl; Closesocket (sserver); //Close SocketWSACleanup ();//release socket resources; return-1; } //Start listening .RetVal = Listen (Sserver,1); if(Socket_error = =retVal) {cout<<"Listen failed!"<<Endl; Closesocket (sserver); //Close SocketWSACleanup ();//release socket resources; return-1; } //Accept client Requestssockaddr_in addrclient; intAddrclientlen =sizeof(addrclient); Sclient= Accept (Sserver, (sockaddr far*) &addrclient, &Addrclientlen); if(Invalid_socket = =sclient) {cout<<"Accept failed!"<<Endl; Closesocket (sserver); //Close SocketWSACleanup ();//release socket resources; return-1; } while(true){ //receiving client DataZeroMemory (buf, buf_size); RetVal= Recv (sclient, buf, Buf_size,0); if(Socket_error = =retVal) {cout<<"recv failed!"<<Endl; Closesocket (sserver); //Close SocketClosesocket (sclient);//Close SocketWSACleanup ();//release socket resources; return-1; } if(buf[0] =='0') Break; cout<<"data sent by the client:"<< buf <<Endl; cout<<"to send data to the client:"; CIN>>SendBuf; Send (Sclient, SendBuf, strlen (sendbuf),0); } //ExitClosesocket (sserver);//Close SocketClosesocket (sclient);//Close SocketWSACleanup ();//release socket resources; return 0;}
Socket (socket)