TCP/UDP Socket Programming Steps

Source: Internet
Author: User
Tags bind set socket socket strlen htons
TCP/UDP Socket Programming Step 2010-11-01 15:04:33

Category: C/

Sockets (socket) programming has three kinds, streaming sockets (SOCK_STREAM), datagram Sockets (SOCK_DGRAM), raw sockets (SOCK_RAW);

TCP/UDP Programming steps in the Windows environment:

1. TCP-based socket programming is the use of streaming sockets.

In this program, you add two projects to a workspace. To link a ws2_32.lib library file.

steps for server-side programming:

1: Load socket font, create socket (WSAStartup ()/socket ());

2: Bind sockets to an IP address and a port (bind ());

3: Set socket to listening mode waiting for connection request (listen ());

4: After the request arrives, accept the connection request and return a new socket corresponding to this connection (accept ());

5: Communicates with the client using the returned socket (send ()/recv ());

6: Return, wait for another connection request;

7: Close the socket and close the loaded socket font (closesocket ()/wsacleanup ()).

The server-side code is as follows:

#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_stream,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));

Listen (socksrv,5);

Sockaddr_in addrclient;

int len=sizeof (SOCKADDR);

while (1)

{

SOCKET sockconn=accept (Socksrv, (sockaddr*) &addrclient,&len);

Char sendbuf[50];

sprintf (SendBuf, "Welcome%s to here!", Inet_ntoa (ADDRCLIENT.SIN_ADDR));

Send (Sockconn,sendbuf,strlen (SENDBUF) +1,0);

Char recvbuf[50];

Recv (sockconn,recvbuf,50,0);

printf ("%s\n", recvbuf);

Closesocket (Sockconn);

}

}

client-side programming steps:

1: Load socket font, create socket (WSAStartup ()/socket ());

2: Make a connection request to the server (connect ());

3: Communicate with the server (send ()/recv ());

4: Close the socket and close the loaded socket font (closesocket ()/wsacleanup ()).

The code for the client is as follows:

#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_stream,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);

Connect (sockclient, (sockaddr*) &addrsrv,sizeof (sockaddr));

Char recvbuf[50];

Recv (sockclient,recvbuf,50,0);

printf ("%s\n", recvbuf);

Send (sockclient, "Hello", strlen ("Hello") +1,0);

Closesocket (sockclient);

WSACleanup ();

}

2. UDP-based socket programming is the datagram socket used.

In this program, you add two projects to a workspace. Also link to a ws2_32.lib library file.

steps for server-side programming:

1: Load socket font, create socket (WSAStartup ()/socket ());

2: Bind sockets to an IP address and a port (bind ());

3: Wait and receive data (SendTo ()/recvfrom ());

4: Close the socket and close the loaded socket font (closesocket ()/wsacleanup ()).

The server-side 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);

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 (7003);

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

Char recvbuf[50];

SOCKADDR addrclient;

int len=sizeof (SOCKADDR);

Recvfrom (socksrv,recvbuf,50,0, (sockaddr*) &addrclient,&len);

printf ("%s\n", recvbuf);

Closesocket (SOCKSRV);

WSACleanup ();

}

for a UDP-based socket client, the following steps are needed:

1: Create a socket (socket);

2: Send data to the server (sendto);

3: Close socket;

The code is as follows:

#include <winsock2.h>

#include <stdio.h>

void Main ()

{

WORD wversionrequested;

Wsadata Wsadata;

int err;

wversionrequested = Makeword (2, 2);

Err = WSAStartup (wversionrequested, &wsadata);

if (err! = 0) {

Return

}

if (Lobyte (wsadata.wversion)! = 2 | |

Hibyte (wsadata.wversion)! = 2) {

WSACleanup ();

Return

}

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

Sockaddr_in addrclient;

Addrclient.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");

Addrclient.sin_family=af_inet;

Addrclient.sin_port=htons (8889);

Sockaddr_in addrsrv;

SendTo (sockclient, "Hi", 3,0, (sockaddr*) &addrclient,sizeof (sockaddr));

}

TCP/UDP Programming steps in the Linux environment:

TCP Programming Steps:

A. Service side:

1.socket (int domain,int type,int protocol): set socket;

2. bind (int sockid,struct sockaddr *addrp,socklen_t addrlen): Bind the native address and port to the socket established in the previous step;

3.listen (int sockid,int qsize): monitors a socket;

4.fd=accept (int sockid,struct sockaddr *callerid,socklen_t *addrlenp): Waits for a socket to receive information;

5.RECV (int fd,void *buf,size_t nbytes,int flags): Receive data from sockets;

6.close (FD) and close (Sockid)

two. Client:

1. Socket (): Set socket;

2.connect (int sockid,struct sockaddr *serv_addrp,socklen_t addrlen): Connect to the server;

3. Send (int sockfd,const void *buf,size_t nbytes,int Flags): Sends data to the server.

4. Close (Sockid);

UDP programming Steps:

One, the service side:

1. Socket (): ibid.;

2. Bind (): ibid.;

3. recvfrom (int sockfd,void*buff,size_t nbytes,int flags,struct sockaddr*from,socklen_t*addrlen): Receive data at socket port, and record the received data source; Be sure to note that the parameter Addrlen here is not only the output of the function, but also the input of the function. So to assign sizeof (struct sockaddr) to Addrlen before calling the function. Otherwise the returned address from will be faulted.

4. Close (SOCKFD);

two. Client:

1. Socket (); ibid.;

2. sendto (int sockfd,const void*buff,size_t nbytes,int flags,const struct sockaddr*to,socklen_t addrlen): Sends data to the specified address;

3. Close (SOCKFD);

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.