一、域名解析
把www.XXX.XXX.COM转换成IP地址
返回值


将域名转化成IP(只适用于IPV4)
1、加#include “netdb.h”
2、结构体变量 struct hostent *hs = NULL;
3、
4、
二、网络属性设置

选项



三、 网络超时优化
方法一:

方法二:用select检测socket是否‘ready’

方法三:设置定时器(timer),捕捉SIGALRM信号


四、广播编程


五、组播


六、UNIX域套接字






服务器
#include <pthread.h>
#include <signal.h>
#include "net.h"
void cli_data_handle (void *arg);
void sig_child_handle(int signo)
{
if(SIGCHLD == signo) {
waitpid(-1, NULL, WNOHANG);
}
}
int main (void)
{
int fd = -1;
signal(SIGCHLD, sig_child_handle);
/* 1. 创建socket fd */
if ((fd = socket (AF_LOCAL, SOCK_STREAM, 0)) < 0) { //改动一
perror ("socket");
exit (1);
}
/*优化4: 允许绑定地址快速重用 */
int b_reuse = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
//填充sockad当然结构体
struct sockaddr_un sun;
bzero(&sun,sizeof(sun));
sun.sun_family = AF_LOCAL;
if(!access(UNIX_DOMAIN_FILE,F_OK))
{
unlink(UNIX_DOMAIN_FILE);
}
strncpy(sun.sun_path,UNIX_DOMAIN_FILE,strlen(UNIX_DOMAIN_FILE));
/*2.2 绑定 */
if (bind (fd, (struct sockaddr *) &sun, sizeof (sin)) < 0) {
perror ("bind");
exit (1);
}
/*3. 调用listen()把主动套接字变成被动套接字 */
if (listen (fd, BACKLOG) < 0) {
perror ("listen");
exit (1);
}
printf ("UINX DEMO Server starting....OK!\n");
int newfd = -1;
/*4. 阻塞等待客户端连接请求 */
while(1) {
pid_t pid = -1;
if ((newfd = accept (fd,NULL,NULL)) < 0) {
perror ("accept");
break;
}
/*创建一个子进程用于处理已建立连接的客户的交互数据*/
if((pid = fork()) < 0) {
perror("fork");
break;
}
if(0 == pid)
{ //子进程中
close(fd);
printf ("Clinet is connected!\n");
cli_data_handle(&newfd);
return 0;
}
else
{ //实际上此处 pid >0, 父进程中
close(newfd);
}
}
close (fd);
return 0;
}
void cli_data_handle (void *arg)
{
int newfd = *(int *) arg;
printf ("Child handling process: newfd =%d\n", newfd);
//..和newfd进行数据读写
int ret = -1;
char buf[BUFSIZ];
char resp_buf[BUFSIZ+10];
while (1) {
bzero (buf, BUFSIZ);
do {
ret = read (newfd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
exit (1);
}
if (!ret) { //对方已经关闭
break;
}
printf ("Receive data: %s\n", buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client(fd=%d) is exiting!\n", newfd);
break;
}
bzero(resp_buf, BUFSIZ+10);
strncpy(resp_buf, SERV_RESP_STR, strlen(SERV_RESP_STR));
strcat(resp_buf, buf);
do {
ret = write(newfd, resp_buf, strlen(resp_buf));
}while(ret < 0 && EINTR == errno);
}
close (newfd);
}
客户端
/*./client unix_domain_file */
#include "net.h"
void usage (char *s)
{
printf ("\n%s unix_domain_file\n\n", s);
}
int main (int argc, char **argv)
{
int fd = -1;
int port = -1;
struct sockaddr_un sun;
if (argc != 2) {
usage (argv[0]);
exit (1);
}
/* 1. 创建socket fd */
if ((fd = socket (AF_LOCAL, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*2.连接服务器 */
/*2.1 填充struct sockaddr_un结构体变量 */
bzero (&sun, sizeof (sun));
sun.sun_family = AF_LOCAL;
/*确保UNIX_DOMAIN_FILE要先存在并且可写,不存在则退出*/
if(access(UNIX_DOMAIN_FILE, F_OK| W_OK) < 0)
{
exit(1);
}
strncpy(sun.sun_path,UNIX_DOMAIN_FILE,strlen(UNIX_DOMAIN_FILE));
if (connect (fd, (struct sockaddr *) &sun, sizeof (sin)) < 0) {
perror ("connect");
exit (1);
}
printf ("unix Client staring...OK!\n");
int ret = -1;
fd_set rset;
int maxfd = -1;
struct timeval tout;
char buf[BUFSIZ];
while (1) {
FD_ZERO (&rset);
FD_SET (0, &rset);
FD_SET (fd, &rset);
maxfd = fd;
tout.tv_sec = 5;
tout.tv_usec = 0;
select (maxfd + 1, &rset, NULL, NULL, &tout);
if (FD_ISSET (0, &rset)) { //标准键盘上有输入
//读取键盘输入,发送到网络套接字fd
bzero (buf, BUFSIZ);
do {
ret = read (0, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
continue;
}
if (!ret)
continue;
if (write (fd, buf, strlen (buf)) < 0) {
perror ("write() to socket");
continue;
}
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client is exiting!\n");
break;
}
}
if (FD_ISSET (fd, &rset)) { //服务器给发送过来了数据
//读取套接字数据,处理
bzero (buf, BUFSIZ);
do {
ret = read (fd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read from socket");
continue;
}
if (!ret)
break; /* 服务器关闭 */
//There is a BUG,FIXME!!
printf ("server said: %s\n", buf);
if ((strlen(buf) > strlen(SERV_RESP_STR))
&& !strncasecmp (buf+strlen(SERV_RESP_STR), QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Sender Client is exiting!\n");
break;
}
}
}
/*4.关闭套接字 */
close (fd);
}
点h文件
#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <sys/select.h>
#include <sys/time.h>
//#include <sys/types.h>
#include <unistd.h>
#include <sys/un.h>
#define UNIX_DOMAIN_FILE "/tmp/my_domain_file.1"
#define BACKLOG 5
#define QUIT_STR "quit"
#define SERV_RESP_STR "SERVER:"
#endif
3930

被折叠的 条评论
为什么被折叠?



