Linux 网络编程---->简单的UDP实例
这是一个简单的UDP的实例而已~
//!> server
//!> server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#define
//!> server
//!> server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#define
SERV_PORT 6000
#define MAXLINE 1024
int main( int argc, char ** argv )
{
char recv[MAXLINE];
int listenfd,connfd;
int chilen, len,i;
int nread;
structsockaddr_in servaddr, chiaddr;
//!> establish the socket
//!>
if( (listenfd = socket( AF_INET, SOCK_DGRAM,0 ) ) == -1 )
{
printf("Socket error.. %d\n", errno);
exit(EXIT_FAILURE );
}
//!>
//!> write info
bzero(&servaddr, sizeof( servaddr ) );
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr =htonl( INADDR_ANY);
servaddr.sin_port = htons( SERV_PORT );
//!>
//!> bind
if( bind(listenfd, ( struct sockaddr * )&servaddr, sizeof(servaddr ) ) == -1 )
{
printf("Binderror... %d\n", errno);
exit(EXIT_FAILURE );
}
while( 1)
{
chilen =sizeof( chiaddr );
memset( recv, 0, sizeof( recv ) );
nread =recvfrom(connfd,recv, MAXLINE, 0, (struct sockaddr*)&chiaddr, &chilen);//read(connfd, recv, MAXLINE );
//!> 记住:一般都用它
if( nread ==-1 )
#define MAXLINE 1024
int main( int argc, char ** argv )
{
char recv[MAXLINE];
int listenfd,connfd;
int chilen, len,i;
int nread;
structsockaddr_in servaddr, chiaddr;
//!> establish the socket
//!>
if( (listenfd = socket( AF_INET, SOCK_DGRAM,0 ) ) == -1 )
{
printf("Socket error.. %d\n", errno);
exit(EXIT_FAILURE );
}
//!>
//!> write info
bzero(&servaddr, sizeof( servaddr ) );
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr =htonl( INADDR_ANY);
servaddr.sin_port = htons( SERV_PORT );
//!>
//!> bind
if( bind(listenfd, ( struct sockaddr * )&servaddr, sizeof(servaddr ) ) == -1 )
{
printf("Binderror... %d\n", errno);
exit(EXIT_FAILURE );
}
while( 1)
{
chilen =sizeof( chiaddr );
memset( recv, 0, sizeof( recv ) );
nread =recvfrom(connfd,recv, MAXLINE, 0, (struct sockaddr*)&chiaddr, &chilen);//read(connfd, recv, MAXLINE );
//!> 记住:一般都用它
if( nread ==-1 )