TCP socket programming for Windows and Linux __ block chain

Source: Internet
Author: User
Tags local time htons

The code under Windows is as follows

Server.cpp #include <iostream> #include <windows.h> #define IP "127.0.0.1" #define PORT 8888 #pragma comment
(Lib, "Ws2_32.lib") using namespace std;
char* GetTime ();
    int main () {wsadata wsadata;
    WORD Ret;  if (Ret = WSAStartup (Makeword (2, 2), &wsadata)!= 0) {cout << "WSAStartup () Error Whit:" <<
        Ret << Endl;
        WSACleanup ();
    return-1;

    }//the unified version type.
    SOCKET Sockser, SOCKCLI;
    Sockser = socket (af_inet,sock_stream,0);
    Sockaddr_in Addrser, ADDRCLI;
    addrser.sin_family = af_inet;
    Addrser.sin_port = htons (port); Addrser.sin_addr. S_un.

    S_ADDR = inet_addr (IP);
    int len = sizeof (SOCKADDR);
    Bind (Sockser, (sockaddr*) &addrser, Len);//bind socket.
    Listen (sockser,5)/monitor.
    cout << "The server is waiting for a connection ..." << Endl;
    Sockcli = Accept (Sockser, (sockaddr*) &addrcli, &len);
if (sockcli = = 1) {cout << "Accept () Error with:" << sockcli << Endl;        Closesocket (SOCKCLI);
        Closesocket (Sockser);
    WSACleanup ();
    } Char serbuff[255];
    string S;
        while (1) {recv (sockcli,serbuff,sizeof (Serbuff), 0);
    Send (Sockcli,gettime (), 30, 0);
    } wsacleanup ();
    Closesocket (Sockser);
    Closesocket (SOCKCLI);
return 0;
    } char* GetTime () {char buff[255];
    SYSTEMTIME Sys;//windows The following system time call.
    Getlocaltime (&sys);//Initialize local time.
    sprintf (Buff, "%d:%d:%d:%d:%d:%d", Sys.wyear,sys.wmonth,sys.wday,sys.whour,sys.wminute,sys.wsecond);
return buff; }
Client.cpp #include <iostream> #include <windows.h> #define PORT 8888 #define IP "127.0.0.1" using namespace

Std
    #pragma comment (lib, "Ws2_32.lib") int main () {DWORD Ret;
    Wsadata Wsadata;  if (Ret = WSAStartup (Makeword (2, 2), &wsadata))!= 0) {cout << "WSAStartup () Error wiht:" <<
        Ret << Endl;
        WSACleanup ();
    return-1;
    Socket SOCKCLI = socket (af_inet,sock_stream,0);
    Sockaddr_in Addrser;
    addrser.sin_family = af_inet;
    Addrser.sin_port = htons (port); Addrser.sin_addr. S_un.

    S_ADDR = inet_addr (IP);
    No binding listening is required.
    int ret = connect (SOCKCLI, (sockaddr*) &addrser,sizeof (sockaddr));
        if (ret!= 0) {cout << "Client connection Failed ..." << Endl;
        Closesocket (SOCKCLI);
        WSACleanup ();
    return-1;
    else {cout << "customer service End Connection Successful ..." << Endl;
    const char timebuff[] = "Times:";
    Char serbuff[255]; while (1) {
        Send (Sockcli,timebuff,strlen (timebuff) +1,0);
        Recv (Sockcli,serbuff,sizeof (Serbuff), 0);
        Gets the local time of the server.
        cout <<timebuff<<serbuff << Endl;
    Sleep (2000);
return 0; }

The following code for Linux is as follows:
Makefile

CLI=CLI
ser=ser
dir=.
cc=g++

ser_obj=$ (dir)/ser.o
cli_obj=$ (dir)/cli.o ser_src=$

(dir)/ser.cpp cli_src=$
(dir)/cli.cpp


all:$ (CLI) $ (SER)
. Phony:all

$ (SER): $ (ser_obj)
    @$ (cc)-O $@ $^

$ (CLI): $ (cli_obj)
    @$ (cc)-O $@ $^

$ (ser_obj): $ ( SER_SRC)
    @$ (cc)-C $<

$ (cli_obj): $ (CLI_SRC)
    @$ (cc)-C $<

. Phony:clean clean
:
    @rm $ (DIR)/*.O $ (SER) $ (CLI)

Ser.cpp

#include <iostream> #include <sys/socket.h> #include <arpa/inet.h> #include <string.h> #

Include <netinet/in.h> #define PORT 8888 #define IP "127.0.0.1" using namespace std;
    int main () {int sockser,sockcli;

    Sockaddr_in addrser,addrcli;
    Sockser = socket (af_inet,sock_stream,0);
    addrser.sin_family = af_inet;
    Addrser.sin_port = htons (port);

    ADDRSER.SIN_ADDR.S_ADDR = inet_addr (IP);
    socklen_t len = sizeof (SOCKADDR);
    Bind (Sockser, (sockaddr*) &addrser,len);
    Listen (sockser,5);

    cout<< "I am waiting ..." <<endl;

    Sockcli = Accept (Sockser, (sockaddr*) &addrcli,&len);
    if (sockcli==-1) {close (sockser);
    Close (SOCKCLI);
    return-1;
    } Char clibuff[255];
        while (1) {recv (sockcli,clibuff,sizeof (Clibuff), 0);
        cout<<clibuff<<endl; if (strncmp ("Quit", clibuff,4) ==0) {cout<< "I am quit Too!"
        <<endl;
   Break     Send (Sockcli,clibuff,strlen (clibuff) +1,0);
    Close (SOCKCLI);
    Close (Sockser);
return 0; }

Cli.cpp

#include <iostream>
#include <string.h>
#include <sys/socket.h>
#include <netinet /in.h>
#include <arpa/inet.h>
using namespace std;
#define PORT 8888
#define IP "127.0.0.1"
int main ()
{
    int sockcli = socket (af_inet,sock_stream,0);
    Sockaddr_in Addrser;
    addrser.sin_family = af_inet;
    Addrser.sin_port = htons (port);
    ADDRSER.SIN_ADDR.S_ADDR = inet_addr (IP);

    socklen_t len = sizeof (SOCKADDR);
    int ret = connect (SOCKCLI, (sockaddr*) &addrser,len);
    if (ret!=0)
    {
    cout<< "connect () Erorr with:" <<ret<<endl;
    Close (SOCKCLI);
    return-1;
    }
    Char serbuff[255];
    while (1)
    {
       cin>>serbuff;
       Send (Sockcli,serbuff,strlen (serbuff) +1,0);
       if (strncmp ("Quit", serbuff,4) ==0)
        {break
        ;
        }
       memset (Serbuff, ' the ", sizeof (Serbuff));    
    }
    Close (SOCKCLI);
    return 0;
}

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.