Linux implementation of TCP Socket

Source: Internet
Author: User

TCP Server:

# Include <iostream>

# Include <cstring>

# Include <sys/types. h>

# Include <sys/socket. h>

# Include <netinet/in. h>

# Include <errno. h>

# Define SRVPORT 10005

# Define CONNECT_NUM 5

# Define MAX_NUM 80

Using namespace std;

Int main (int argc, char * argv [])

{

Int aSock =-1, srcSock =-1;

Int nsvaddr =-1, cnt =-1;

Sockaddr_in sa, serverSockAddr;

Sockaddr srvSockAddr;

Memset (& srvSockAddr, 0, sizeof (srvSockAddr ));

ASock = socket (AF_INET, SOCK_STREAM, 0 );

If (aSock <0)

{

Cout <"Create srcSock Error! "<Endl;

Return 0;

}

Memset (& serverSockAddr, 0, sizeof (serverSockAddr ));

Memset (& sa, 0, sizeof (sa ));

Sa. sin_family = AF_INET;

Sa. sin_port = htons (u_short) SRVPORT );

Sa. sin_addr.s_addr = htonl (INADDR_ANY );

If (bind (aSock, (sockaddr *) & sa, sizeof (sa) <0)

{

Cout <"Bind Error! "<Endl;

Return 0;

}

If (listen (aSock, CONNECT_NUM) <0)

{

Cout <"Listen Error! "<Endl;

Return 0;

}

Cout <"Server has been opened:" <endl;

While (true)

{

Cnt =-1;

Nsvaddr = sizeof (sockaddr_in );

SrcSock = accept (aSock, (sockaddr *) & serverSockAddr, (socklen_t *) & nsvaddr );

If (srcSock <0)

{

Cout <"Server accept Error @" <endl;

Cout <"srcSock is:" <srcSock <endl;

Cout <"Error No is:" <errno <endl;

Perror ("server :");

Return 0;

}

Char szRevMeg [MAX_NUM] = {0 };

Char szSdMeg [MAX_NUM] = {"Hello World! "};

Cout <"Begin to read:" <endl;

Cnt = read (srcSock, szRevMeg, MAX_NUM );

If (cnt <0)

{

Cout <"Read Error! "<Endl;

Return 0; // Just to test

}

Cout <"cnt is:" <cnt <endl;

Cout <"Complete Read:" <endl;

Cout <szRevMeg <endl;

Cnt = write (srcSock, szSdMeg, strlen (szSdMeg) + 1 );

If (cnt <0)

{

Cout <"Write Error! "<Endl;

}

Cout <"Write Completed! "<Endl;

Close (srcSock );

Cout <endl;

}

Close (aSock );

Return 0;

}

TCP Client:

# Include <iostream>

# Include <cstring>

# Include <sys/types. h>

# Include <sys/socket. h>

# Include <netinet/in. h>

# Include <arpa/inet. h>

# Define PORT 10005

# Define SRVIP "127.0.0.1" // "222.26.209.141"

# Define RCVNUM 1024

Using namespace std;

Int main ()

{

Int clientSock, nRecvBytes =-1;

Sockaddr_in sa;

Char Buffer [RCVNUM] = {0 };

ClientSock = socket (AF_INET, SOCK_STREAM, 0 );

If (clientSock <0)

{

Cout <"Client Socket create error! "<Endl;

Return 0;

}

Memset (& sa, 0, sizeof (sa ));

Sa. sin_family = AF_INET;

Sa. sin_port = htons (PORT );

Sa. sin_addr.s_addr = inet_addr (SRVIP );

If (connect (clientSock, (sockaddr *) & sa, sizeof (sockaddr) <0)

{

Cout <"Connect Error! "<Endl;

Return 0;

}

Cout <"Client has been opened:" <endl;

Strcpy (Buffer, "Here client said: hello ");

Cout <"Begin to write:" <endl;

Write (clientSock, Buffer, strlen (Buffer) + 1 );

Cout <"Write completed" <endl;

Memset (Buffer, 0, sizeof (Buffer ));

If (nRecvBytes = read (clientSock, Buffer, RCVNUM) <0)

{

Cout <"Read Message Error! "<Endl;

Return 0;

}

Cout <"Server says:" <Buffer <endl;

Close (clientSock );

Cout <"End of file" <endl;

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.