Socket (send, receive)

Source: Internet
Author: User
Tags socket error htons

Send. cpp:

#include <WinSock2.h>#include <stdio.h>#include <Windows.h>#pragma comment(lib,"ws2_32.lib")int main(){    unsigned long ip;    ip=inet_addr("127.0.0.1");    short port;    port=8000;    printf("Connecting to 127.0.0.1:8000....");    WSADATA wsa;    if (WSAStartup(MAKEWORD(2,2),&wsa)!=0)    {        printf("socket init error");        exit(-1);    }    SOCKET sock;    if ((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_ATOM)    {        printf("create socket error");        exit(-1);    }    struct sockaddr_in serverAddress;    memset(&serverAddress,0,sizeof(sockaddr_in));    serverAddress.sin_family=AF_INET;    serverAddress.sin_addr.S_un.S_addr=ip;    serverAddress.sin_port=htons(port);    if (connect(sock,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR)    {        printf("link error");        exit(-1);    }    char buf[4096];    while (1)    {        printf(">");        gets(buf);        if (send(sock,buf,strlen(buf),0)==SOCKET_ERROR)        {            printf("send error");            exit(-1);        }        int bytes;        if ((bytes=recv(sock,buf,sizeof(buf),0))==SOCKET_ERROR)        {            printf("receive error");            exit(-1);        }        buf[bytes]='\0';        printf("Message from %s:%s\n",inet_ntoa(serverAddress.sin_addr),buf);    }    WSACleanup();    return 0;}

Receive. cpp:

# Include <winsock2.h> # include <stdio. h> # include <windows. h> # pragma comment (Lib, "ws2_32.lib") int main (INT argc, char * argv []) {short port; Port = 8000; wsadata WSA; if (wsastartup (makeword (2, 2), & WSA )! = 0) {printf ("socket init error"); exit (-1);} socket serversocket; If (serversocket = socket (af_inet, sock_stream, ipproto_tcp )) = invalid_socket) {printf ("failed to create socket! "); Exit (-1);} struct sockaddr_in serveraddress; memset (& serveraddress, 0, sizeof (sockaddr_in); serveraddress. sin_family = af_inet; serveraddress. sin_addr.s_un.s_addr = htonl (inaddr_any); serveraddress. sin_port = htons (port); If (BIND (serversocket, (sockaddr *) & serveraddress, sizeof (serveraddress) = socket_error) {printf ("socket port error: % d ", Port); exit (-1);} If (Listen (serversocket, somaxconn) = socket_e Rror) {printf ("Listen error"); exit (-1);} printf ("server 8000 is listening ...... \ n "); socket clientsocket; struct sockaddr_in clientaddress; memset (& clientaddress, 0, sizeof (clientaddress); int addrlen = sizeof (clientaddress ); if (clientsocket = accept (serversocket, (sockaddr *) & clientaddress, & addrlen) = invalid_socket) {printf ("Link error"); exit (-1 );} char Buf [4096]; while (1) {int bytes; If (Tes = Recv (clientsocket, Buf, sizeof (BUF), 0) = socket_error) {printf ("data error"); exit (-1 );} buf [bytes] = '\ 0'; printf ("Message from % s: % s \ n", inet_ntoa (clientaddress. sin_addr), Buf); If (send (clientsocket, Buf, bytes, 0) = socket_error) {printf ("failed to send data! "); Exit (-1) ;}} wsacleanup (); exit (-1 );}

Other people's programs need to use socket in the project

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.