Linux C Programming-udp

Source: Internet
Author: User

For more information about Linux C Programming, see

For socket programming, refer:

Socket programming:

Http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html

Http://www.kuqin.com/networkprog/20080512/8361.html

Network byte sequence and conversion functions

Http://blog.sina.com.cn/s/blog_4ad7c25401019qqb.html

1. Set socket

2. Read socket status

3. UDP client implementation

4. UDP server implementation



1. Set socket

2. Read socket status

#include<sys/types.h>#include<sys/socket.h>#include<stdio.h>int  main(){  int s;  int val=1,len,i ;  len= sizeof(int);  if((s = socket(AF_INET,SOCK_STREAM,0))<0)    //creat  {    perror("connect");    exit(1);  }  else  {    printf("a socket was created.\n");    printf("socket number:%d\n",s);   }   i=setsockopt(s,SOL_SOCKET,SO_TYPE,&val,len);   //set   if("i==0")   {     printf("set socket ok.\n.");    }    else   {     printf("set socket error.\n.");    }    getsockopt(100,SOL_SOCKET,SO_TYPE,&val,&len);  //get   perror("socket");}

3. UDP client implementation

Some compilers report errors when using ADDR (struct sockaddr *) & ADDR

# Include <stdio. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <unistd. h> # include <fcntl. h> # include <sys/STAT. h> # include <sys/types. h> # include <sys/socket. h> # define remoteport 4567 # define remoteip "127.0.0.1" int main (INT argc, char * argv []) {int S, Len; struct sockaddr_in ADDR; int addr_len; char MSG [256]; int I = 0; If (S = socket (af_inet, sock_dgram, 0) <0) // create a socket {perror ("error "); Exit (1);} else {printf ("socket created. \ n "); printf (" socked ID: % d \ n ", S); printf (" remote IP: % s \ n ", remoteip ); printf ("remote port: % d \ n", remoteport);} Len = sizeof (struct sockaddr_in); // length bzero (& ADDR, sizeof (ADDR); ADDR. sin_family = af_inet; // Add the port and address ADDR. sin_port = htons (remoteport); ADDR. sin_addr.s_addr = inet_addr (remoteip); While (1) {bzero (MSG, sizeof (MSG); Len = read (stdin_fileno, MSG, Si Zeof (MSG); sendto (S, MSG, Len, 0, & ADDR, addr_len); // send the message printf ("\ ninput message: % s \ n ", MSG); Len = recvfrom (S, MSG, sizeof (MSG), 0, & ADDR, & addr_len);/* This is the received information. */Printf ("% d:", I); I ++; printf ("received message: % s \ n", MSG);/* this is the information returned by the server. */}}

4. UDP server implementation

Some compilers report errors when using ADDR (struct sockaddr *) & ADDR

# Include <stdio. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <unistd. h> # include <sys/types. h> # include <sys/socket. h> # define localport 4567int main (INT argc, char * argv []) {int mysock, Len; struct sockaddr_in ADDR; int I = 0; char MSG [256]; int addr_len; If (mysock = socket (af_inet, sock_dgram, 0) <0) {perror ("error"); exit (1 );} else {printf ("socket created. \ n "); printf (" socked ID: % d \ n ", mysock);} addr_len = sizeof (struct sockaddr_in); bzero (& ADDR, sizeof (ADDR )); ADDR. sin_family = af_inet; ADDR. sin_port = htons (localport); ADDR. sin_addr.s_addr = htonl (inaddr_any); If (BIND (mysock, & ADDR, sizeof (ADDR) <0) {perror ("Connect"); exit (1 );} else {printf ("bind OK. \ n "); printf (" local port: % d \ n ", localport);} while (1) {bzero (MSG, sizeof (MSG )); len = recvfrom (mysock, MSG, sizeof (MSG), 0, & ADDR, & addr_len);/* received information */printf ("% d:", I ); I ++; printf ("Message from: % s \ n", inet_ntoa (ADDR. sin_addr); printf ("message length: % d \ n", Len); printf ("message: % s \ n", MSG); sendto (mysock, MSG, Len, 0, & ADDR, addr_len);/* The above strings are returned to the client */}}

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.