Between Linux systems, network programming, message sending and receiving

Source: Internet
Author: User
Tags htons

Between Linux systems, network programming, message sending and receiving

[Email protected]:~/udp$ sudo apt-get update[email protected]:~/udp$ sudo apt-get install Build-essential[email protected]:~/udp$ sudo apt-get install make
[Email protected]:~/udp$ ll-rw-rw-r--1 Chunli Chunli 279 may 10:36 makefile-rw-rw-r--1 Chunli Chunli 2.3K May 15 10: myudp.cpp-rw-rw-r--1 Chunli Chunli (4) 10:37 myudp.h-rw-rw-r--1 Chunli Chunli 259 may 10:37 Stdafx.cpp files, compile a library file, the source code is below the text:
[Email protected]:~/udp$ makeg++-wall-fpic-d__unix-o myudp.o-c myudp.cppg++-o libmyudp.so myudp.o-shared ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ OK ^_^ ^_^ ^_^, ^_^ ^_^ ^_^

Write a main program

vim main.c#include <stdio.h> #include  <stdlib.h> #include  <string.h># include  "Myudp.h" Int main (int argc, char args[]) {         if  (argc < 2)         {                 bind_socket (8080);                 char buf[1024] =  { 0 };                 char IP[100] = { 0 };                 memset (buf, 0, sizeof (BUF));                 recv_socket (Buf, sizeof (BUF), &NBSP;IP);                 printf ("from %s :  %s \n ", Ip,buf);        } else         {                 char buf[1024] = {  "hello world!"  };                send_ Socket ("127.0.0.1",  8080, buf, strlen (BUF));         }         return 0;}
compile [email protected]:~/udp$ gcc main.c-l.-lmyudp Send to native [email protected]:~/udp$./a.out 127.0.0.1 waiting to receive [email protected]:~ /udp$./a.outfrom 127.0.0.1:hello world!












Source:

[Email protected]:~/udp$ cat makefile. Suffixes:. cpp. occ=g++srcs=myudp.cppobjs=$ (SRCS:.CPP=.O) Out=libmyudp.soall: $ (OBJS) $ (CC)-O $ (out) $ (OBJS)-  Shared@echo ' ^_^ ^_^ ^_^ ^_^ ^_^ ^_^ OK ^_^ ^_^ ^_^ ". CPP.O: $ (CC)-wall-fpic-d__unix-o [email protected]-C $<clean:-rm-f $ (OBJS)-rm-f core*




[email protected]:~/udp$ cat myudp.cpp // myudp.cpp :  definition  DLL  The export function of the application. #include  <string.h> #include   "stdafx.h" #ifndef  __unix#include <winsock2.h># Pragma comment (lib,  "ws2_32.lib") #pragma  warning (disable:4996) #else #include <sys/ types.h> #include  <sys/socket.h> #include  <unistd.h> #include  <arpa/inet.h># Include <netinet/in.h> #define  socket int#endifsocket st = 0;// Global socket Variable Void init_sosket () {if  (st == 0) {#ifndef  __UNIXunsigned short ver; Wsadata wsadata;ver = makeword (1, 1); WSAStartup (ver, &wsadata);//Initialize socketchar on = 1; #elseint  on = 1;# Endifst = socket (af_inet, sock_dgram, 0);//Establish udp socketsetsockopt (St, SOL_SOCKET ,  so_broadcast, &on, sizeof (on));//set to send UDP broadcast messages}}void free_socket () {if  (ST) {#ifndef  __unixclosesocket (ST); WSACleanup (); #elseclose (ST); #endifst  = 0;}} extern  "C" {#ifndef  __unix__declspec (dllexport) #endifint  bind_socket (unsigned short  Port) {init_sosket (); Struct sockaddr_in recv_addr;memset (&recv_addr, 0, sizeof (recv_ addr)); Recv_addr.sin_family = af_inet;recv_addr.sin_port = htons (port);// Specifies that port is the port number to connect to recv_addr.sin_addr.s_addr = htonl (inaddr_any);//Specifies that the receive end IP address is any return bind (st,  (struct sockaddr *)  &recv_addr, sizeof (RECV_ADDR));} #ifndef  __unix__declspec (dllexport) #endifunsigned  int recv_socket (char *buf, size_t &NBSP;BUFSIZE,&NBSP;CHAR&NBSP;*SRCIP) {Struct sockaddr_in addr;memset (&addr, 0, sizeof ( addr)); #ifndef  __unixint len = sizeof (addr); #elsesocklen_t  len = sizeof ( addr); #endifunsigned  int rc = recvfrom (St, buf, bufsize, 0,  (struct sockaddr *)  &addr, &len);// Received a message from Srcip strcpy (Srcip, inet_ntoa (ADDR.SIN_ADDR)); RETURN&NBSP;RC;} #ifndef  __unix__declspec (dllexport) #endifunsigned  int send_socket (Const char *destip,  unsigned short port, const char *buf, size_t bufsize) {init_sosket (); Struct sockaddr_in addr;memset (&addr, 0, sizeof (addr)); addr.sin_family = af_ Inet;addr.sin_port = htons (port);//Specify Port as the port number to connect to ADDR.SIN_ADDR.S_ADDR&NBSP;=&NBSP;INET_ADDR (DestIP) ;//Specify IP address to connect to Return sendto (st, buf, bufsize, 0,  (struct sockaddr *)  &addr, sizeof (addr));//Send message to specified IP}}
[Email protected]:~/udp$ cat myudp.h #ifndef __myudp_h#define __myudp_hint bind_socket (unsigned short port); unsigned int Recv_socket (char *buf, size_t bufsize, char *srcip); unsigned int send_socket (const char *destip, unsigned short port, cons T Char *buf, size_t bufsize); #endif
[Email protected]:~/udp$ cat stdafx.h//stdafx.h: The standard system contains file containing files,//or frequently used but infrequently changed//project-specific Include files//#ifndef __unix#pragma Onc E#include "Targetver.h" #define WIN32_LEAN_AND_MEAN//Exclude very little used information from the Windows header file//Windows header file: #include <win Dows.h> #endif//TODO: Refer to other header files required by the program here





This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1773543

Between Linux systems, network programming, message sending and receiving

Related Article

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.