简单Linux网络编程实现回射。此处服务器端server.c
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <stdio.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include <fcntl.h>
-
#include <sys/shm.h>
-
-
#define MYPORT 8887
-
#define QUEUE 20
-
#define BUFFER_SIZE 2048
-
void do_service(int conn) /*定义一个do_service结构体,进行输出和输入操作。*/
-
{
-
char buffer[BUFFER_SIZE];
-
while(1)
-
{
-
memset(buffer,0,sizeof(buffer)); /*初始化*/
-
int len = recv(conn, buffer, sizeof(buffer),0);/*接收到的数据*/
-
if(len==0)
-
{
-
printf("client close\n");
-
break;
-
}
-
else if(len==-1)
-
{
-
perror("read");
-
}
-
fputs(buffer, stdout); /*标准输出*/
-
send(conn, buffer, len, 0); /*标准发送*/
-
}
-
}
-
int main()
-
{
-
///定义sockfd
-
int server_sockfd = socket(AF_INET,SOCK_STREAM, 0);
-
-
///定义sockaddr_in
-
struct sockaddr_in server_sockaddr;
-
server_sockaddr.sin_family = AF_INET;
-
server_sockaddr.sin_port = htons(MYPORT);
-
server_sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
-
-
///bind,成功返回0,出错返回-1
-
int on =1;
-
if(setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))==-1) /*获得套接口信息*/
-
{
-
perror("setsockopt");
-
exit(1);
-
}
-
printf("ip=%s port=%d\n",inet_ntoa(server_sockaddr.sin_addr),ntohs(server_sockaddr.sin_port));
-
if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1)
-
{
-
perror("bind");
-
exit(1);
-
}
-
-
///listen,成功返回0,出错返回-1
-
int listenfd=listen(server_sockfd,QUEUE) ; /*建立监听*/
-
if(listenfd<0)
-
{
-
perror("listen");
-
exit(1);
-
}
-
-
///客户端套接字
-
-
struct sockaddr_in client_addr;
-
socklen_t length = sizeof(client_addr);
-
-
///成功返回非负描述字,出错返回-1
-
printf("accept ...\n");
-
int conn ;
-
pid_t pid;/*用fork()创建子进程*/
-
while(1) /*可以让多个客户端连接*/
-
{ if((conn = accept(server_sockfd, (struct sockaddr*)&client_addr, &length))<0) /*conn 为接收套接字*/
-
{
-
perror("connect");
-
exit(1);
-
-
}
-
-
-
pid =fork();
-
if(pid==-1)
-
{
-
perror("fork");
-
exit(1);
-
}
-
if(pid==0) /*子进程进行函数操作*/
-
{
-
close(server_sockfd);
-
do_service(conn);
-
exit(0);
-
}
-
else
-
close(conn); /*父进程进行函数操作*/
-
-
-
}
-
return 0;
-
}
客户端client.c
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <stdio.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include <fcntl.h>
-
#include <sys/shm.h>
-
-
#define MYPORT 8887
-
#define BUFFER_SIZE 2048
-
-
int main()
-
{
-
///定义sockfd
-
int sock_cli = socket(AF_INET,SOCK_STREAM, 0);
-
-
///定义sockaddr_in
-
struct sockaddr_in servaddr;
-
memset(&servaddr, 0, sizeof(servaddr)); /*初始化*/
-
servaddr.sin_family = AF_INET; ///IPv4协议
-
servaddr.sin_port = htons(MYPORT); ///服务器端口
-
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); ///服务器ip
-
-
///连接服务器,成功返回0,错误返回-1
-
if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
-
{
-
perror("connect");
-
exit(1);
-
}
-
printf("ip=%s port=%d\n",inet_ntoa(servaddr.sin_addr),ntohs(servaddr.sin_port)); ///输出服务器IP地址和端口
-
-
char sendbuf[BUFFER_SIZE];
-
char recvbuf[BUFFER_SIZE];
-
while (fgets(sendbuf, sizeof(sendbuf), stdin) != NULL) ///键盘输入
-
{
-
send(sock_cli, sendbuf, strlen(sendbuf),0); ///发送
-
recv(sock_cli, recvbuf, sizeof(recvbuf),0); ///接收
-
fputs(recvbuf, stdout);
-
-
memset(sendbuf, 0, sizeof(sendbuf));
-
memset(recvbuf, 0, sizeof(recvbuf));
-
}
-
-
close(sock_cli);
-
return 0;
-
}
阅读(1301) | 评论(0) | 转发(0) |