linux socket小例
/*
socket select模型,服务端
绝大多数注释自己写的,参考man
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/un.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <netinet/in.h>
#define SERVPORT 3333
#define BACKLOG 10
#define MAX_CONNECTED_NO 10
#define MAXDATASIZE 100
int main()
{
/*
struct sockaddr_in {
//地址族AF_INET(IPv4)AF_INET6(IPv6)AF_LOCAL(UNIX域协议)AF_LINK(链路地址协议)AF_KEY(密钥套接字socket)AF_APPLETALK(ddp)
short int sin_family;
//端口号
unsigned short int sin_port;
*
* struct in_addr {
* unsigned long s_addr;
* }
*
//地址
struct in_addr sin_addr;
unsigned char sin_zero[8];
}
eg:
struct sockaddr_in ina;
bzero(&ina,sizeof(ina));
ina.sin_family=AF_INET;
ina.sin_port=htons(23);
ina.sin_addr.s_addr = inet_addr("132.241.5.10");
AF_INET和PF_INET略有差异
*/
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size,recvbytes;
fd_set readfd;
fd_set writefd;
int sockfd,client_fd;
char buf[MAXDATASIZE];
/*
int socket(int domain, int type, int protocol);
domain:PF_INET...
type:SOCK_STREAM,SOCK_STREAM,SOCK_RAW...
protocol:/etc/protocols tcp,udp...
*/
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
printf("socket success!,sockfd=%d\n",sockfd);
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(SERVPORT);
server_sockaddr.sin_addr.s_addr=INADDR_ANY;
//set to 0
bzero(&(server_sockaddr.sin_zero),8);
/*
int bind(int sockfd, const struct sockaddr *my_addr,socklen_t addrlen);
sockfd:created by socket
*my_addr:convert from sockaddr_in
addrlen:sizeof(sockaddr)
*/
if(bind(sockfd,(
相关文档:
这一节我们来看看其他线程函数:
int pthread_tryjoin_np(pthread_t thread_handle, void ** thread_return);
int pthread_timedjoin_np (pthread_t thread_handle, void **thread_return, __const struct timespec *abstime);
pthread_tryjoin_np会可以用来判断thread_handle指向的线程是否已经中止,如果没有则*thre ......
这里继续上一篇关于线程函数的介绍:
int pthread_detach (pthread_t thread_handle);
这个函数的作用是让thread_handle指向的目标线程在中止的时候清理自己拥有的数据。在这个函数调用之后,
不能再对thread_handle使用pthread_join。
显而易见的是,如果我们没有对新创建的线程调用pthread_detach,这个线 ......
在linux环境下编程有一个很让人头疼的问题就是信号对系统调用的干扰,在系统调用过程中如果受到信号的干扰,
部分系统调用会将errno设置成EINTR,我们不得不编写下面的错误处理代码来防止这种情况的发生:
syscall:
int ret = syscall(...);
if (ret < 0 && EINTER == errno)
goto syscall;
el ......
安装前准备好两个分区A 和B,分区 A 用来存放下载来的 Fedora 10 的ISO镜像文件,分区 B 用来安装 Fedora 10 .。
注意:存放镜像文件的分区必须为 Fat32 格式,否则无法进行安装。(本人已测试过)
解压 Fedora-10-i386-netinst.iso 此ISO 文件,将解压出来的 isolinux 和 images 两个文件夹与 Fedora 10 的 ISO 镜像文 ......