Socket编程实例:
服务器端:一直监听本机的8000号端口,如果收到连接请求,将接收请求并接收客户端发来的消息,并向客户端返回消息。
客户端:client.c
/* File Name: client.c */ #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h> #define MAXLINE 4096 int main(int argc, char** argv)
{
int sockfd, n,rec_len;
int i_port = ; //默认8000端口 char recvline[], sendline[];
char buf[MAXLINE];
char *c_ipAddr = "127.0.0.1"; struct sockaddr_in servaddr; if( argc == )
{
printf("This client will connect server message: IP=127.0.0.1 , Port=8000 \n");
}
else if( argc == )
{
c_ipAddr = argv[];
printf("This client will connect server message: IP=%s , Port=8000 \n",c_ipAddr);
}
else if( argc == )
{
c_ipAddr = argv[];
i_port = atoi(argv[]);
printf("This client will connect server message: IP=%s , Port=%d \n",c_ipAddr, i_port);
}
else
{
printf("usage: ./client <ipaddress> and port \n");
exit();
} if( (sockfd = socket(AF_INET, SOCK_STREAM, )) < ){
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
exit();
} memset(&servaddr, , sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(i_port); if( inet_pton(AF_INET, c_ipAddr, &servaddr.sin_addr) <= ){
printf("inet_pton error for %s\n",argv[]);
exit();
} if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < ){
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
exit();
} printf("send msg to server: \n");
fgets(sendline, , stdin);
if( send(sockfd, sendline, strlen(sendline), ) < )
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit();
}
if((rec_len = recv(sockfd, buf, MAXLINE,)) == -) {
perror("recv error");
exit();
}
buf[rec_len] = '\0';
printf("Received : %s ",buf);
close(sockfd);
exit();
}
服务端:server.c
/* File Name: server.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h> #define DEFAULT_PORT 8000 //监听端口号
#define MAXLINE 4096
int main(int argc, char** argv)
{
int socket_fd, connect_fd;
struct sockaddr_in servaddr;
char buff[];
int n;
//初始化Socket
if( (socket_fd = socket(AF_INET, SOCK_STREAM, )) == - ){
printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
exit();
}
//初始化
memset(&servaddr, , sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);//IP地址设置成INADDR_ANY,让系统自动获取本机的IP地址。
servaddr.sin_port = htons(DEFAULT_PORT);//设置的端口为DEFAULT_PORT //将本地地址绑定到所创建的套接字上
if( bind(socket_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -){
printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
exit();
}
//开始监听是否有客户端连接
if( listen(socket_fd, ) == -){
printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
exit();
}
printf("======waiting for client's request======\n");
while(){
//阻塞直到有客户端连接,不然多浪费CPU资源。
if( (connect_fd = accept(socket_fd, (struct sockaddr*)NULL, NULL)) == -){
printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
continue;
}
//接受客户端传过来的数据
n = recv(connect_fd, buff, MAXLINE, );
//向客户端发送回应数据
if(!fork()){ /*子进程*/
if(send(connect_fd, "Hello,you are connected!\n", ,) == -)
perror("send error");
close(connect_fd);
exit();
}
buff[n] = '\0';
printf("recv msg from client: %s\n", buff);
close(connect_fd);
}
close(socket_fd);
}
编译:
客户端 gcc -o client client.c
服务端 gcc -o server server.c
运行:(俩个窗口运行,一个服务端一个客户端)
服务端 ./server
客户端 有3种运行方式
1. ./client (无ip无端口 使用默认的 IP=127.0.0.1 , Port=8000 地址 )
2. ./client 192.168.109.133 (添加ip 无端口 使用默认端口 8000)
3. ./client 192.168.109.133 10101 (使用指定的ip和端口)
演示:默认方式
客户端:
服务端: