Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the socket encapsulation method of C++

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the socket encapsulation method of C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Configure the pro file under QT

TEMPLATE = app

CONFIG + = console

CONFIG-= app_bundle

CONFIG-= qt

LIBS + =-lWs2_32 # # Mark using Ws2_32.lib,-l under window to indicate that you want to link to the following libraries

#-lWs2_32,link Ws2_32.lib

SOURCES + = main.cpp\

Udp.cpp

Include (deployment.pri)

QtcAddDeployment ()

HEADERS + =\

Udp.h

two。 Write udp.h files

# ifndef UDP_H

# define UDP_H

# ifdef MYLINUX

# include

# include

# include / * See NOTES * /

# include

# define SOCKET int

# else

# include

# endif

Class myudp

{

Private:

The inner members of the SOCKET st;// class are generally not exposed.

Public:

Myudp ()

~ myudp ()

Int socket_send (const char * IP,const char * buf, int len)

Int socket_recv (char * buf, int len, char * srcIP)

Int socket_bind (short int port)

}

# endif / / UDP_H

3. Write udp.cpp

# include

# include

# include "udp.h"

/ / # define MYLINUX-definition of makefile file under linux-DMYLINUX

Int myudp::socket_send (const char * IP,const char * buf, int len)

{

St = socket (AF_INET, SOCK_DGRAM, 0)

/ / create a socket. The first parameter is to specify which protocol to use for socket. AF_INET stands for using TCP/IP protocol.

/ / the second parameter SOCK_DGRAM means to use the UDP protocol

/ / the third parameter is generally 0 by default.

Struct sockaddr_in addr

Memset (& addr, 0, sizeof (addr)); / / initialize the structure addr

Addr.sin_family = AF_INET;// represents the address of a TCP/IP to be used

Addr.sin_port = htons (8080); / / host to net short

Addr.sin_addr.s_addr = inet_addr (IP)

/ / IP address resolution

/ / unsigned long laddr = inet_addr ("192.168.6.200")

/ / unsigned char * p = & laddr

/ / printf ("% u,% u\ n", * (p), * (packs 1), * (packs 2), * (packs 3))

Size_t rc = 0

/ / send udp data

Rc = sendto (st,buf,len,0, (struct sockaddr *) & addr,sizeof (addr))

Return rc

}

Int myudp::socket_recv (char * buf, int len, char * srcIP)

{

Struct sockaddr_in sendaddr;// comes from the sender's IP address

Memset (& sendaddr,0,sizeof (sendaddr))

# ifdef MYLINUX

Socklen_t len

# else

Int addrlen

# endif

Addrlen = sizeof (sendaddr)

Memset (buf,0,len)

Size_t rc = recvfrom (st,buf,len,0, (struct sockaddr *) & sendaddr,&addrlen)

/ / inet_ntoa (sendaddr.sin_addr); / / this function is non-reentrant

/ / multithreaded call, this function will have a problem

If (srcIP)

Strcpy (srcIP,inet_ntoa (sendaddr.sin_addr))

/ / printf ("% s:\ n% s\ n", srcIP, buf)

Return rc

}

Myudp::myudp ()

{

# ifndef MYLINUX

/ / initialize socket

DWORD ver

WSADATA wsaData

Ver = MAKEWORD (1J1); / / when calling WASStartup, I want to tell windows what version of socket I use

WSAStartup (ver, & wsaData); / / windows requires that as long as you use socket, the first step is to call this function.

/ / initialization completed

# endif

St = socket (AF_INET,SOCK_DGRAM,0); / / define a socket

}

Myudp::~myudp ()

{

# ifdef MYLINUX

Close (st)

# else

Closesocket (st); / / close socket

WSACleanup (); / / release related resources within win socket

# endif

}

Int myudp::socket_bind (short port)

{

Struct sockaddr_in addr

Memset (& addr,0,sizeof (addr)); / / initialize addr

Addr.sin_family = AF_INET;// represents the address of a TCP/IP to be used

Addr.sin_port = htons (port)

Addr.sin_addr.s_addr = htonl (INADDR_ANY); / / as the receiver, I do not need to specify a specific IP address. I will accept data at whatever IP the host is accepted.

/ / addr.sin_addr.s_addr = inet_addr ("192.168.2.100")

Return bind (st, (struct sockaddr *) & addr, sizeof (addr)); / / bind the port number to the program

}

The implementation code of 4.main.c

# include

# include "udp.h"

Using namespace std

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

{

If (argc > 1)

{

Myudp udp

Char buf [1024] = {0}

While (1)

{

Memset (buf, 0, sizeof (buf))

Gets (buf)

Udp.socket_send (args [1], buf, strlen (buf))

If (strcmp (buf, "exit") = = 0)

Break

}

} else

{

Myudp udp

Char buf [1024] = {0}

If (udp.socket_bind (8080) >-1)

{

Char ip [100] = {0}

While (1)

{

Memset (buf, 0, sizeof (buf))

Memset (ip, 0, sizeof (ip))

Udp.socket_recv (buf,sizeof (buf), ip)

If (strncmp (buf, "exit", 4) = = 0)

Break

Cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report