Winsock

zhaozj2021-02-16  152

Server source code # include #include "winsock2.h" void main () {// Initialize Winsock. Wsadata wsadata; int innet = wsastartup (MakeWord (2, 2), & WSADATA); if (IResult! = NO_ERROR) printf ( "Error at WSAStartup () / n"); // Create a socket SOCKET m_socket;. m_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); if (m_socket == INVALID_SOCKET) {printf ( "Error at socket ( :% Ld / n ", wsagetlasterror ()); wsacleanup (); return;} // bind the socket. Sockaddr_in service; service.sin_family = AF_IT; service.sin_addr.s_addr = inet_addr (" 127.0.0.1 "); Service.sin_port = htons (27015); if (bind (m_socket, (sockaddr *) & service, sizeof (service)) == SOCKET_ERROR) {Printf ("Bind () failed./n"); clossoSocket (m_socket); Return } // listen on the socket. If (listen (m_socket, 1) == Socket_ERROR) Printf ("ERROR LISTENING ON Socket./N"); // Accept Connections. Socket ACCEP tSocket; printf ( "Waiting for a client to connect ... / n"); while (1) {AcceptSocket = SOCKET_ERROR; while (AcceptSocket == SOCKET_ERROR) {AcceptSocket = accept (m_socket, NULL, NULL);} printf ( "Client Connected./N"); m_socket = acceptsocket; break;} // send and receivers; int BytesRecv = Socket_ERROR; Char SendBuf [32] = "Server: sending data."; Char recvbuf [32] = ""; BytesRecv = Recv (m_socket, recvbuf, 32, 0); Printf ("BYTES RECV:% LD / N", bytesRecv);

BYTESSENT = Send (M_Socket, Sendbuf, Strlen (Sendbuf), 0); Printf ("Bytes Sent:% LD / N", BYTESSENT); RETURN;}

转载请注明原文地址:https://www.9cbs.com/read-8583.html

New Post(0)