Network Programming under Windows

Source: Internet
Author: User
Tags sin socket error htons

Client-side Programs
#include "stdafx.h" #include <winsock2. h> #include <stdio.h> #pragma comment (lib, "Ws2_32.lib") int main (int argc, char* argv[]) {WORD sockversion = MAK    Eword (2,2);     Wsadata data;    if (WSAStartup (sockversion, &data)! = 0) {return 0;    } Socket sclient = socket (af_inet, sock_stream, ipproto_tcp);        if (sclient = = Invalid_socket) {printf ("INVALID SOCKET!");    return 0;    } sockaddr_in seraddr;    seraddr.sin_family = af_inet;    Seraddr.sin_port = htons (8888); Seraddr.sin_addr. S_un.     S_ADDR = inet_addr ("127.0.0.1");        if (Connect (sclient, (SOCKADDR *) &seraddr, sizeof (seraddr)) = = Socket_error) {printf ("Connect ERROR!");        Closesocket (sclient);    return 0;    } char * senddata = "Hello, tcp server, I am client!\n";    Send (Sclient, SendData, strlen (SendData), 0);    Char recdata[255];    int ret = recv (sclient, Recdata, 255, 0);        if (Ret > 0) {Recdata[ret] = 0x00;    printf (recdata); } CLOSESOCKet (sclient);    WSACleanup (); return 0;}
Server-side programs:
<pre name= "code" class= "OBJC" > #include "stdafx.h" #include <stdio.h> #include <winsock2.h> #pragma    Comment (lib, "Ws2_32.lib") int main (int argc, char* argv[]) {//initialize WSA WORD sockversion = Makeword (2,2);    Wsadata Wsadata;    if (WSAStartup (sockversion, &wsadata)!=0) {return 0;    }//Create socket Socket Slisten = socket (af_inet, sock_stream, ipproto_tcp);        if (Slisten = = Invalid_socket) {printf ("SOCKET error!");    return 0;    }//Bind IP and port sockaddr_in sin;    sin.sin_family = af_inet;    Sin.sin_port = htons (8888); Sin.sin_addr. S_un.     S_ADDR = Inaddr_any;    if (Bind (Slisten, (lpsockaddr) &sin, sizeof (sin)) = = Socket_error) {printf ("Bind ERROR!");        }//Start listening if (Listen (Slisten, 5) = = Socket_error) {printf ("Listen ERROR!");    return 0;    }//Loop receive data SOCKET sclient;    Sockaddr_in remoteaddr;    int naddrlen = sizeof (REMOTEADDR);     Char revdata[255]; while (true) {printf ("Wait for connection ... \ n ");        Sclient = Accept (Slisten, (SOCKADDR *) &remoteaddr, &naddrlen);            if (sclient = = Invalid_socket) {printf ("Accept error!");        Continue                } printf ("Accepted to a connection:%s \ r \ n", Inet_ntoa (REMOTEADDR.SIN_ADDR));                Receive data int ret = recv (sclient, Revdata, 255, 0);            if (Ret > 0) {Revdata[ret] = 0x00;        printf (revdata); }//Send data char * senddata = "Hello, TCP client!"        \ n ";        Send (Sclient, SendData, strlen (SendData), 0);    Closesocket (sclient);    } closesocket (Slisten);    WSACleanup (); return 0;}



Network Programming under Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.