TCP的socket的服务器端和客户端实例:
1、服务器端步骤:
socket()->bind()->listen()->accept()->read()->write()->close()
2、客户端步骤:
socket()->connect()->write()->read()->close()
服务器端代码:
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <strings.h>
#include <ctype.h>
char host_name[20];
int port = 8000;
int main()
{
struct sockaddr_in sin,pin;
int sock_descriptor,temp_sock_descriptor,address_size;
int i , len , on=1;
char buf[16384];
sock_descriptor = socket(AF_INET,SOCK_STREAM,0);
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
if(bind(sock_descriptor,(struct sockaddr *)&sin,sizeof(sin)) == -1)
{
perror("call to bind");
exit(1);
}
if(listen(sock_descriptor,100) == -1)
{
perror("call to listem");
exit(1);
}
printf("Accpting connections...\n");
while(1)
{
address_size = sizeof(pin);
temp_sock_descriptor = accept(sock_descriptor,(struct sockaddr *)&pin,&address_size);
if(temp_sock_descriptor == -1)
{
perror("call to accept");
exit(1);
}
if(recv(temp_sock_descriptor,buf,16384,0) == -1)
{
perror("call to recv");
exit(1);
}
inet_ntop(AF_INET,&pin.sin_addr,host_name,sizeof(host_name));
printf("received from client(%s):%s\n",host_name,buf);
len = strlen(buf);
for(i = 0 ; i < len ; i++)
{
buf[i] = toupper(buf[i]);
}
if(send(temp_sock_descriptor,buf,len+1,0) == -1)
{
perror("call to send");
exit(1);
}
close(temp_sock_descriptor);
}
}
客户端代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <strings.h>
#include <ctype.h>
char * host_name = "127.0.0.1";
int port = 8000;
int main(int argc , char * argv[])
{
char buf[8192];
//char message[256];
int socket_descriptor;
struct sockaddr_in pin;
char * str ="A default test string";
if(argc < 2)
{
printf("we will send a default test string.\n");
}
else
{
str = argv[1];
if(argc == 3)
{
host_name = argv[2];
}
}
bzero(&pin,sizeof(pin));
pin.sin_family = AF_INET;
inet_pton(AF_INET,host_name,&pin.sin_addr);
pin.sin_port = htons(port);
if((socket_descriptor = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("error opening socket \n");
exit(1);
}
if(connect(socket_descriptor,(struct sockaddr * )&pin,sizeof(pin)) == -1)
{
perror("error connecting to socket \n");
exit(1);
}
printf("sending message %s to server ..\n",str);
if( write(socket_descriptor,str,strlen(str)+1) == -1 )
{
perror("error in send \n");
exit(1);
}
printf("..sent message ...wait for message..\n");
if( read(socket_descriptor,buf,8192) == -1 )
{
perror("error in receiving response from server \n");
exit(1);
}
printf("\nResponse from server:\n\n%s\n",buf);
close(socket_descriptor);
return 1;
}
运行结果:
服务器端:
[root@rac2 ~]# ./server_iter
Accpting connections...
received from client(127.0.0.1):A default test string
received from client(127.0.0.1):yang
客户端:
[root@rac2 ~]# ./client_iter
we will send a default test string.
sending message A default test string to server ..
..sent message ...wait for message..
Response from server:
A DEFAULT TEST STRING
[root@rac2 ~]# ./client_iter yang 127.0.0.1
sending message yang to server ..
..sent message ...wait for message..
Response from server:
YANG
[root@rac2 ~]#