Professional Documents
Culture Documents
1.1
1
1 Socket
Socket
Socket
Socket
2 2 Socket
Socket Socket Socket
Socket Socket
Socket
Socket
Socket Socket
socket Socket
Socket
Socket
Socket 3G 4G
Socket
Socket 3G CPU
Socket
Socket Socket
Socket
Socket Socket
Socket 1.1
Socket
- 1 -
Socket
Socket
socket( )
Socket
socket( )
Socket
bind( )
Socket
listen( )
connect( )
accept( )
read( )
write( )
write( )
read( )
close( )Socket
close( )Socket
1.1
1.1 Socket
Socket
1.1
1.1 Socket
Socket
1.1 9
Linux
1.1
int main()
{
int server_fd,client_fd;
int server_len,client_len;
struct sockaddr_in server_address;
- 2 -
struct sockaddr_in client_address;
char back[] = {"Im server"};
/* Socket*/
server_fd = socket(AF_INET,SOCK_STREAM,0);
/* Socket IP IP 192.168.1.1
9266*/
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("192.168.1.1");
server_address.sin_port = htons(9266);
server_len = sizeof(server_address);
/* Socket
*/
bind(server_fd,(struct sockaddr*)&server_address,server_len);
/* Socket 10 Socket */
listen(server_fd,10);
while(1) {
char recv[20];
printf("server is waiting\n");
/* Socket
client_address ID */
client_len = sizeof(client_address);
client_fd = accept(server_fd,(struct sockaddr*)&client_address,&client_len);
/* read write */
read(client_fd,recv,20);
write(client_fd,back,20);
printf("received from client= %s\n",recv);
close(client_fd);
}
close(server_fd);
exit(0);
}
1.1
1.1 1.1
C
TCP/IP
Linux
Linux
- 3 -
Linux Linux
1.2
/IP
1TCP/IP
TCP/IP 1.2 TCP/IP
Telnet Email
TCP UDP
IP
1.2
/IP
1.2
RealTek 8169 DM9000 CS8900
TCP/UDP
TCP (Transmission Control Protocol ) UDP(User Datagram Protocol
)TCP IP
UDP IP TCP
UDP TCP
Telnet(Internet )FTP()SMTP()
UDP NFSSNMPDNS
TFTP
Linux TCP
TCP
- 4 -
TCP
TCP
TCP
IP
IP
1.3
/IP
1.2 TCP
1.3
socket()
#include <errno.h>
#include <sys/socket.h>
/* Create a new socket of type TYPE in domain DOMAIN, using
protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
Returns a file descriptor for the new socket, or -1 for errors. */
int
__socket (domain, type, protocol)
int domain;
int type;
- 5 -
int protocol;
{
__set_errno (ENOSYS);
return -1;
}
_socket
ENTRY (__socket)
ENTRY (__socket)
- 6 -
lea 4(%esp), %ecx
#define __NR_socketcall
102
SOCKOP_socket glibc-2.3.6/sysdeps/unix/sysv/linux/socketcall.h
#define SOCKOP_socket
system_call
ENTRY(system_call)
syscall_call:
call *sys_call_table(,%eax,4)
1.4 system_call
call sys_call_table 102 sys_call_table
arch/x86/kernel/syscall_table_32.S
1.5
sys_call_table
ENTRY(sys_call_table)
.long sys_socketcall
/*102*/
2010 unsigned long a0, a1;
2017
2018
2024
a0 = a[0];
2025
a1 = a[1];
2026
2027 switch (call) {//
2028
2029
case SYS_SOCKET:
err = sys_socket(a0, a1, a[2]);
}
sys_socketcall Linux Socket
ebx ecx call Socket ebx 1
2028 SYS_SOCKET
args ecx Linux
copy_from_user
copy_from_user nargs[] call
1.7 nargs[]
1990 #define AL(x) ((x) * sizeof(unsigned long))
1991 static const unsigned char nargs[18]={
1992
AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1993
AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
1994
AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)
1995 };
call AL 12
copy_from_user args AF_INETSOCK_STREAM0
a
include/linux/net.h call
glibc sysdeps/unix/sysv/linux socketcall.h
Linux glibc Linux glibc
1.8
call
32
33
34
35
36
37
38
39
*/
- 8 -
40
*/
41
*/
42
43
44
45
46
47
48
2
sys_socketcall
1.1 socket
sys_socket Socket
-->
sys_socketcall()-->sys_socket()
1216 asmlinkage long sys_socket(int family, int type, int protocol)
1217 {
1221
1225
retval = sock_map_fd(sock);//
1236 }
1221 sock_create Socket
family AF_INETtype SOCK_STREAMprotocol 0
sock_map_fd Socket
1.4
sock_create
sock_fs_type 2.6.26
net/socket.c
1.9
302
303
.name = "sockfs",
304
.get_sb = sockfs_get_sb,
305
.kill_sb = kill_anon_super,
306
sock_fs_type
};
file_system_type Linux
- 9 -
file_system_type
745
746
do_one_initcall(*call);//
750 }
696 static void __init do_one_initcall(initcall_t fn)
697 {
708
result = fn();//
736 }
do_one_initcall initcall initcall
GCC
__initcall_start __initcall_end do_one_initcall initcall
socket.c core_initcall sock_init initcall
1.10
sock_init
core_initcall
168
#define __define_initcall(level,fn,id) \
169
170
180
191
#define device_initcall(fn)
__define_initcall("6",fn,6)
196
251
module_init
initcall sock_init
sock_init
kernel_init()-->do_basic_setup()-->do_initcalls()-->do_one_initcall()-->sock_init()
2157 static int __init sock_init(void)
- 10 -
2158 {
2174
init_inodecache();
2175
register_filesystem(&sock_fs_type);//
2176
sock_mnt = kern_mount(&sock_fs_type);//
2186 }
sock_init sock_fs_type
Socket register_filesystem
Linux kern_mount Linux
sock_fs_type get_sb
293
294
295
296
297
298
mnt);//
}
get_sb_pseudo sock_fs_type
socket
sockfs_ops sockfs_ops
Socket
1.12
286
sockfs_ops
287
.alloc_inode = sock_alloc_inode,
288
.destroy_inode =sock_destroy_inode,
289
.statfs = simple_statfs,
290
};
Linux Socket
sockfs_ops alloc_inode sock_alloc_inode
get_sb_pseudo Socket
- 11 -