Linux系统编程和网络编程,更新ing.......

Socket


网络/主机字节序转换

#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

IP/网络字节序转换

#include <arpa/inet.h>

const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);

int inet_pton(int af, const char *src, void *dst);

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int inet_aton(const char *cp, struct in_addr *inp);

in_addr_t inet_addr(const char *cp);               /*ip点分式转网络字节序*/

in_addr_t inet_network(const char *cp);

char *inet_ntoa(struct in_addr in);

in_addr_t inet_lnaof(struct in_addr in);

in_addr_t inet_netof(struct in_addr in);

//将点分十进制ip转网络字节序可以直接:
myaddr.sin_addr.s_addr = inet_addr(INADDR_ANY);
//或
myaddr.sin_addr.s_addr = INADDR_ANY;

socket函数

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
int socket(int domain, int type, int protocol);

bind函数

#include <sys/socket.h>
int bind(int socket, const struct sockaddr *address, socklen_t address_len);

listen函数

#include <sys/socket.h>
int listen(int socket, int backlog);

<说明>
允许同时连接数

accept函数

#include <sys/socket.h>
int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);

connect函数

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

recv()

参考:https://blog.csdn.net/mercy_ps/article/details/82224128

流程

在这里插入图片描述在这里插入图片描述在这里插入图片描述


nc/telnet命令

nc 和 telnet一样起到客户端的作用
nc ip porttelnet ip port
就可以连服务端


标准I/O

fread

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

从给定输入流stream读取最多count个size大小的对象到数组ptr中
返回成功读取的对象个数,若出现错误或到达文件末尾,则可能小于count。

fseek

int fseek( FILE *stream, long offset, int origin );

第一个参数stream为文件指针
第二个参数offset为偏移量,正数表示正向偏移,负数表示负向偏移
第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CURSEEK_ENDSEEK_SET
SEEK_SET: 文件开头
SEEK_CUR: 当前位置
SEEK_END: 文件结尾
其中SEEK_SET,SEEK_CURSEEK_END依次为012.
简言之:
fseek(fp,100L,0);把stream指针移动到离文件开头100字节处;
fseek(fp,100L,1);把stream指针移动到离文件当前位置100字节处;
fseek(fp,-100L,2);把stream指针退回到离文件结尾100字节处。

ftell

long ftell(FILE *stream);

函数 ftell 用于得到文件位置指针当前位置相对于文件首的偏移字节数。
使用fseek函数后再调用函数ftell()就能非常容易地确定文件的当前位置。
因为ftell返回long型,根据long型的取值范围-231~231-1-21474836482147483647),故对大于2.1G的文件进行操作时出错。
使用:
利用函数 ftell() 也能方便地知道一个文件的长。如以下语句序列:
fseek(fp, 0L,SEEK_END);
len =ftell(fp); 
首先将文件的当前位置移到文件的末尾,然后调用函数ftell()获得当前位置相对于文件首的位移,该位移值等于文件所含字节数。

进程

fork()


进程通信

flock

线程

线程概念及标识

  • posix线程
  • 线程标识:pthread_t
  • ps axm命令来查看线程

线程创建

								/*比较tid*/
#include <pthread.h>
int pthread_equal(pthread_t t1, pthread_t t2); 

							/*获取当前线程标识*/
#include <pthread.h>
pthread_t pthread_self(void)

								/*线程创建*/
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine) (void *), void *arg);

/*
<说明>

<参数>:
	参数1:回填tid
	参数2:指定线程属性
	参数3:线程函数返回值void*类型
	参数4:线程函数的参数
      
<返回值>
*/

线程终止和栈清理

								/*线程终止*/
#include <pthread.h>
void pthread_exit(void *retval);


/*
<参数>:
	retval 返回给 join的 reval

*/
								/*线程收尸*/
#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);

/*
<参数>:
	参数2:状态
*/
								/*栈清理*/
#include <pthread.h>
void pthread_cleanup_push(void (*routine)(void *), void *arg);
void pthread_cleanup_pop(int execute);

线程取消

#include <pthread.h>
int pthread_cancel(pthread_t thread);
/*
<说明>:
	回收一个正在运行的线程,先cancel,再join
	
<参数>:
	线程id

<返回值>:


<补充>:
	线程有两种取消选项
		允许:
			- 异步
			- 推迟
		不允许:
			-
*/


include <pthread.h>
int pthread_setcancelstate(int state, int *oldstate);
int pthread_setcanceltype(int type, int *oldtype);

/*
<说明>:
	第一个设置是否取消
	第二个设置取消方式
<>
*/

#include <pthread.h>
void pthread_testcancel(void);

/*
<说明>:
	就是一个取消点什么也不做
*/

								/*线程分离*/
#include <pthread.h>
int pthread_detach(pthread_t thread);

线程同步

互斥量

pthread_mutex_t

#include <pthread.h>
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

条件变量

#include <pthread.h>
int pthread_cond_destroy(pthread_cond_t *cond);

int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
broadcast icmp jiqiao multi_broad raw sys_net_ml tcp1 term thread udp cap io mul_thread_download qq sem tcp tcp_dos testerr time wav ./broadcast: broad.h client.c server server.c ./cap: capture_packet capture_packet.c ./icmp: icmp.c ./io: aa ba conf filename.c fwrite.c print.c scandir.c setvbuf test tree1.c vprintf ab bb conf.c fwrite print scandir server.conf setvbuf.c tree tree.c vprintf.c ./io/test: a abb b c d ./io/test/d: e f g ./jiqiao: client client.c udp_server udp_server.c ./mul_thread_download: down.c download download-注释.c readme.txt ./multi_broad: client.c mul.h server.c ./qq: ansi_code.c client gen_single_nohead_pre.c Makefile server tcp.h ansi_code.h client.c gen_single_nohead_pre.h play_sub.c server.c ansi_code.o client.o gen_single_nohead_pre.o play_sub.h server.o ./raw: gethostbyname.c ping1.c ping.c zs.c ./sem: semget_1.c semget.c ./tcp: fork qq simple ./tcp/fork: client client.c server server.c tcp.h ./tcp/qq: client client.c gen_single_nohead_pre.h server.c client1.cv gen_single_nohead_pre.c server tcp.h ./tcp/simple: client client.c makefile server server.c tcp.h ./tcp1: fork ftp simple simple1 ./tcp1/fork: client.c server.c tcp.h ./tcp1/ftp: client.c ftp.h server.c ./tcp1/simple: client client.c makefile server server.c tcp.h ./tcp1/simple1: client1.c client2.c client.c server1.c server2.c server.c ./tcp_dos: tcp_dos tcp_dos.c ./term: getpass getpass.c gets gets.c translate ./term/translate: ansi_code.c ansi_code.o chin.c dict dict.o glist.c glist.o input.h Makefile ansi_code.h chin ciku.dict dict.c glist1.c glist.h input.c input.o tags ./testerr: testerr_copyclient.c testerr_copyserver.c testerrno_copyserver1.c ./thread: aa bping.c hello pthread_attr.c pthread_create pthread_mutex.c while.c aa.c chain hello.c pthread_attr.s pthread_create1.c pthread_rwlock aa.h cond pthread_attr pthread_cond pthread_create.c pthread_rwlock.c bping cond.c pthread_attr1.c pthread_cond.c pthread_mutex while ./thread/chain: chain_queue.c chain_queue_hanoi chain_queue.o hanoi.c main.c chain_queue.h chain_queue_hanoi.c hanoi main ./time: asctime ctime gettimeofday strftime time time.c asctime.c ctime.c gettimeofday.c strftime.c timea.c ./udp: client client_echo client_loop reliable_udp server_echo.c server_loop.c udp_server client1 client_echo.c client_loop.c server server_hello stack client1.c client_hello htonl server.c server_hello.c stack.c client.c client_hello.c htonl.c server_echo server_loop ts ./udp/reliable_udp: reliable_recv reliable_recv_std reliable_send reliable_send_std reliable_recv.c reliable_recv_std.c reliable_send.c reliable_send_std.c ./wav: exec line.c lrc_display makefile play.c playwav.c setitimer1.c getline llist.c lrc_display.c mp3 play_sub.c playwav.h setitimer.c getline.c llist.h main.c play play_sub.h setitimer ./wav/mp3:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值