mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-04-02 10:22:22 +08:00
9.1 SO_SNDBUF & SO_RCVBUF
This commit is contained in:
1
ch09/README.md
Normal file
1
ch09/README.md
Normal file
@@ -0,0 +1 @@
|
||||
## 第 9 章 套接字的多种可选项
|
||||
34
ch09/get_buf.c
Normal file
34
ch09/get_buf.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
void error_handling(char *message);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sock;
|
||||
int snd_buf, rcv_buf, state;
|
||||
socklen_t len;
|
||||
|
||||
sock = socket(PF_INET, SOCK_STREAM, 0);
|
||||
len = sizeof(snd_buf);
|
||||
state = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *)&snd_buf, &len);
|
||||
if (state)
|
||||
error_handling("getsockopt() error");
|
||||
|
||||
len = sizeof(rcv_buf);
|
||||
state = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&rcv_buf, &len);
|
||||
if (state)
|
||||
error_handling("getsockopt() error");
|
||||
|
||||
printf("Input buffer size: %d \n", rcv_buf);
|
||||
printf("Output buffer size: %d \n", snd_buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
void error_handling(char *message)
|
||||
{
|
||||
fputs(message, stderr);
|
||||
fputc('\n', stderr);
|
||||
exit(1);
|
||||
}
|
||||
36
ch09/sock_type.c
Normal file
36
ch09/sock_type.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
void error_handling(char *message);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int tcp_sock, udp_sock;
|
||||
int sock_type;
|
||||
socklen_t optlen;
|
||||
int state;
|
||||
|
||||
optlen = sizeof(sock_type);
|
||||
tcp_sock = socket(PF_INET, SOCK_STREAM, 0);
|
||||
udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
printf("SOCK_STREAM: %d\n", SOCK_STREAM);
|
||||
printf("SOCK_DGRAM: %d\n", SOCK_DGRAM);
|
||||
|
||||
state = getsockopt(tcp_sock, SOL_SOCKET, SO_TYPE, (void *)&sock_type, &optlen);
|
||||
if (state)
|
||||
error_handling("getsockopt() error");
|
||||
printf("Socket type one: %d \n", sock_type);
|
||||
|
||||
state = getsockopt(udp_sock, SOL_SOCKET, SO_TYPE, (void *)&sock_type, &optlen);
|
||||
if (state)
|
||||
error_handling("getsockopt() error");
|
||||
printf("Socket type two: %d \n", sock_type);
|
||||
return 0;
|
||||
}
|
||||
void error_handling(char *message)
|
||||
{
|
||||
fputs(message, stderr);
|
||||
fputc('\n', stderr);
|
||||
exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user