socket()函数创建一个socket,然后调用bind()函数将其与本机的地址以及一个本地端口绑定~然后利用listen()在相应的socket上监听,当accept()接收到一个连接的服务请求时,服务器将显示客户机的ip地址,并通过新的socket向客户端发送字符串”Hello,you are connected!”最后关闭socket
myserver.c:
客户端通过服务器域名获得服务器的ip,然后新建一个socket调用connect与服务器连接,连接成功后接收从服务器发送过来的数据,最后关闭socket
gethostbyname()是完成域名转换的 struct hostent gethostbyname(const char name)
server
(myserver.c) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
*socket server
* */
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/wait.h>
#define SERVERPORT 3333
#define BACKLOG 10
int main ()
{
int sockfd , clientfd ;
struct sockaddr_in my_addr ;
struct sockaddr_in remote_addr ;
if (( sockfd = socket ( AF_INET , SOCK_STREAM , 0 )) == - 1 ){
perror ( "socket create error !!!" );
exit ( 1 );
}
my_addr . sin_family = AF_INET ;
my_addr . sin_port = htons ( SERVERPORT );
my_addr . sin_addr . s_addr = INADDR_ANY ;
bzero ( & ( my_addr . sin_zero ), 8 );
if ( bind ( sockfd ,( struct sockaddr * ) & my_addr , sizeof ( struct sockaddr )) == - 1 ){
perror ( "bind error...." );
exit ( 1 );
}
if ( listen ( sockfd , BACKLOG ) == - 1 )
{
perror ( "listen error .." );
exit ( 1 );
}
while ( 1 ){
int sin_size = sizeof ( struct sockaddr_in );
if (( clientfd = accept ( sockfd ,( struct sockaddr * ) & remote_addr , & sin_size )) ==- 1 ){
perror ( "accept error..." );
continue ;
}
printf ( "Receied a connection from %s \n " , inet_ntoa ( remote_addr . sin_addr ));
if ( ! fork ()){
if ( send ( clientfd , "Hello,you are connected! \n " , 26 , 0 ) == - 1 )
perror ( "send error..." );
close ( clientfd );
exit ( 0 );
}
close ( clientfd );
}
}
client
(myclient.c) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
*socket client
* */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define SERVERPORT 3333
#define MAXDATASIZE 100
int main ( int argc , char * argv [])
{
int sockfd , recvbytes ;
char buf [ MAXDATASIZE ];
struct hostent * host ;
struct sockaddr_in serv_addr ;
if ( argc < 2 )
{
fprintf ( stderr , "Please enter the server'hostname! \n " );
exit ( 1 );
}
if (( host = gethostbyname ( argv [ 1 ])) == NULL ){
perror ( "gethostbyname error..." );
exit ( 1 );
}
if (( sockfd = socket ( AF_INET , SOCK_STREAM , 0 )) == - 1 )
{
perror ( "socket error..." );
exit ( 1 );
}
serv_addr . sin_family = AF_INET ;
serv_addr . sin_port = htons ( SERVERPORT );
serv_addr . sin_addr = * (( struct in_addr * ) host -> h_addr );
bzero ( & ( serv_addr . sin_zero ), 8 );
if ( connect ( sockfd ,( struct sockaddr * ) & serv_addr , sizeof ( struct sockaddr )) == - 1 )
{
perror ( "connext error..." );
exit ( 1 );
}
if (( recvbytes = recv ( sockfd , buf , MAXDATASIZE , 0 )) == - 1 )
{
perror ( "recv error ...." );
exit ( 1 );
}
buf [ recvbytes ] = '\0' ;
printf ( "REceived:%s \n " , buf );
close ( sockfd );
}