Socket programming (UDP TCP) code memo C++/c__linux

Source: Internet
Author: User
Tags set socket sprintf htons
Sockets (socket) programming has three kinds, streaming socket (sock_stream), datagram Socket (SOCK_DGRAM), the original socket (SOCK_RAW); Socket programming based on TCP is a streaming socket (sock_stream). Datagram Sockets (SOCK_DGRAM) based on UDP.
Programming steps for 1.TCP streaming sockets
You must link library functions before using: Engineering-> set->link-> input Ws2_32.lib,ok.
Server-side programs:
1, Load Socket font
2, create socket (socket).
3. Bind the socket to a local address and port (BIND).
4. Set socket to listening mode, prepare to receive customer request (listen).
5, waiting for the arrival of the customer request, when the request arrives, accept the connection request and return a new socket (accept) corresponding to this connection.
6, with the return socket and client communication (SEND/RECV).
7, return, wait for another customer request.
8, close the socket.
Client program:
1, Load Socket font
2, create socket (socket).
3, send a connection request to the server (connect).
4, and the server side of the communication (SEND/RECV).
5, close the socket.
The server-side code is as follows:
#include <winsock2.h>//header File
#include <stdio.h>//load standard input Output header file

void Main ()
{
WORD wversionrequested;//Version number
Wsadata Wsadata;
int err;

wversionrequested = Makeword (1, 1);//1.1 version of Socket

Err = WSAStartup (wversionrequested, &wsadata);
if (Err!= 0) {
Return
}//load Socket font, the addition of failure to return

if (Lobyte (wsadata.wversion)!= 1 | |
Hibyte (wsadata.wversion)!= 1) {
WSACleanup ();
Return
}//, if it's not 1.1, quit.
Socket Socksrv=socket (af_inet,sock_stream,0);//create socket (socket).

Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_addr=htonl (Inaddr_any);//convert unsigned short to network byte-order format
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port=htons (6000);

Bind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr));
Bind a socket to a local address and port (BIND)
Listen (socksrv,5);//set Socket to listener mode, ready to receive client request (listen).

Sockaddr_in addrclient;//defines the address family
int len=sizeof (SOCKADDR),//Initialize this parameter, this parameter must be initialized

while (1)
{
SOCKET sockconn=accept (Socksrv, (sockaddr*) &addrclient,&len); The third parameter of the accept must have an initial value.
Wait for the customer request to arrive, and when the request arrives, accept the connection request and return a new socket (accept) corresponding to this connection.
This is where the program is blocking.
Char sendbuf[100];
sprintf (SendBuf, "Welcome%s to http://www.sunxin.org",
Inet_ntoa (ADDRCLIENT.SIN_ADDR));
Communicates with the returned socket and the client (SEND/RECV).
Send (Sockconn,sendbuf,strlen (SENDBUF) +1,0);
Char recvbuf[100];
Recv (sockconn,recvbuf,100,0);
printf ("%s\n", recvbuf);
Closesocket (sockconn);//close socket. Wait for another user to request
}
}

The client code is as follows:
#include <Winsock2.h>
#include <stdio.h>

void Main ()
{
WORD wversionrequested;
Wsadata Wsadata;
int err;

wversionrequested = Makeword (1, 1);

Err = WSAStartup (wversionrequested, &wsadata); Load socket libraries
if (Err!= 0) {
Return
}

if (Lobyte (wsadata.wversion)!= 1 | |
Hibyte (wsadata.wversion)!= 1) {
WSACleanup ();
Return
}
Socket Sockclient=socket (af_inet,sock_stream,0), create socket (socket).

Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port=htons (6000);
Connect (sockclient, (sockaddr*) &addrsrv,sizeof (sockaddr)), issue a connection request to the server (connect).

Char recvbuf[100]; communication with the server side (SEND/RECV).
Recv (sockclient,recvbuf,100,0);
printf ("%s\n", recvbuf);
Send (Sockclient, "This are Lisi", strlen ("This is Lisi") +1,0);

Closesocket (sockclient), closing sockets.
WSACleanup ();//must call this function to clear the argument
}

Type 2.UDP socket.
Server-side (receiving-side) programs:
1, create socket (socket).
2. Bind the socket to a local address and port (BIND).
3, waiting to receive data (RECVFROM).
4, close the socket.
Client (send-side) program:
1, create socket (socket).
2, send data to the server (SendTo).
3, close the socket.
Server-side code:
#include <Winsock2.h>
#include <stdio.h>

void Main ()
{
WORD wversionrequested;
Wsadata Wsadata;
int err;

wversionrequested = Makeword (1, 1);

Err = WSAStartup (wversionrequested, &wsadata);
if (Err!= 0) {
Return
}

if (Lobyte (wsadata.wversion)!= 1 | |
Hibyte (wsadata.wversion)!= 1) {
WSACleanup ();
Return
}

SOCKET Socksrv=socket (af_inet,sock_dgram,0);
Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_addr=htonl (Inaddr_any);
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port=htons (6000);

Bind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr));

Sockaddr_in addrclient;
int len=sizeof (SOCKADDR);
Char recvbuf[100];

Recvfrom (socksrv,recvbuf,100,0, (sockaddr*) &addrclient,&len);
printf ("%s\n", recvbuf);
Closesocket (SOCKSRV);
WSACleanup ();
}

Client code:
#include <Winsock2.h>
#include <stdio.h>

void Main ()
{
WORD wversionrequested;
Wsadata Wsadata;
int err;

wversionrequested = Makeword (1, 1);

Err = WSAStartup (wversionrequested, &wsadata);
if (Err!= 0) {
Return
}

if (Lobyte (wsadata.wversion)!= 1 | |
Hibyte (wsadata.wversion)!= 1) {
WSACleanup ();
Return
}

SOCKET Sockclient=socket (af_inet,sock_dgram,0);
Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port=htons (6000);

SendTo (sockclient, "Hello", strlen ("Hello") +1,0,
(sockaddr*) &addrsrv,sizeof (sockaddr));
Closesocket (sockclient);
WSACleanup ();
}
=========== below is a simple UDP chat program under the character interface =====
Server side: ==============================
#include <stdio.h>
#include <winsock2. H>

void Main ()
{
WORD wversionrequested;
Wsadata Wsadata;
int err;

wversionrequested = Makeword (1, 1);

Err = WSAStartup (wversionrequested, &wsadata);
if (Err!= 0)
{
Return
}
if (Lobyte (wsadata.wversion)!= 1 | |
Hibyte (wsadata.wversion)!= 1)
{
WSACleanup ();
Return
}

SOCKET Socksrv=socket (af_inet,sock_dgram,0);

Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_addr=htonl (Inaddr_any);
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port=htons (6101);

Bind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr));

Char sendbuf[100];
Char recvbuf[100];
Char tempbuf[100];
int len=sizeof (SOCKADDR);
Sockaddr_in addrclient;
while (1)
{

Recvfrom (Socksrv,tempbuf,strlen (TEMPBUF), 0, (sockaddr*) &addrclient,&len);
if (' Q '!=tempbuf[0])
{
sprintf (Recvbuf, "%s say:%s", Inet_ntoa (ADDRCLIENT.SIN_ADDR), tempbuf);
printf ("%s\n", recvbuf);
printf ("Please input your data:");
Gets (SENDBUF);
SendTo (Socksrv,sendbuf,strlen (SENDBUF) +1,0, (sockaddr*) &addrclient,len);
}
Else
{
printf ("%s request to quit the chat platform.\n",
Inet_ntoa (ADDRCLIENT.SIN_ADDR));
SendTo (socksrv, "Q", strlen ("Q") +1,0, (sockaddr*) &addrclient,len);
Break
}
}
Closesocket (SOCKSRV);
WSACleanup ();

}

Client: ===============================
#include <stdio.h>
#include <winsock2. H>

void Main ()
{
WORD wversionrequested;
Wsadata Wsadata;
int err;

wversionrequested = Makeword (1, 1);

Err = WSAStartup (wversionrequested, &wsadata);
if (Err!= 0)
{
Return
}
if (Lobyte (wsadata.wversion)!= 1 | |
Hibyte (wsadata.wversion)!= 1)
{
WSACleanup ();
Return
}
SOCKET Sockclient=socket (af_inet,sock_dgram,0);

Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");
htonl htons inet_addr Inet_ntoa
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port=htons (6101);

Char sendbuf[100];
Char tempbuf[100];
Char recvbuf[100];
int len=sizeof (SOCKADDR);

while (1)
{
printf ("Please input your data:");
Gets (SENDBUF);
SendTo (Sockclient,sendbuf,strlen (SENDBUF) +1,0, (sockaddr*) &addrsrv,len);
Recvfrom (Sockclient,tempbuf,strlen (TEMPBUF), 0, (sockaddr*) &addrsrv,&len);
if (' Q '!=tempbuf[0])
{
sprintf (Recvbuf, "%s say:%s", Inet_ntoa (ADDRSRV.SIN_ADDR), tempbuf);
printf ("%s\n", recvbuf);
}
Else
{
printf ("The server has been closed!\n");
SendTo (sockclient, "Q", strlen ("Q") +1,0, (sockaddr*) &addrsrv,len);
Break
}

}

Closesocket (sockclient);
WSACleanup ();

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.