自己动手写客户端程序
同样是实现上篇的功能,客户端程序不再用telnet,自己写代码实现,服务器端程序也需要修改下
- 服务器程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
void error(char *msg)
{
fprintf(stderr, "%s : %s\n", msg, strerror(errno) );
exit(1);
}
int open_listener_socket(void)
{
int s = socket(PF_INET, SOCK_STREAM, 0); //创建服务器Socket , Ipv4协议,面向连接通信,TCP协议
if (s == -1)
error("scoket open error");
return s;
}
void bind_to_port(int socket, int port)
{
struct sockaddr_in name; //服务器网络地址结构体
name.sin_family = PF_INET;
name.sin_port = (in_port_t)htons(port); // 服务器端口号
name.sin_addr.s_addr = htonl(INADDR_ANY); //服务器地址,允许连接到所有本地连接上
// 设置socket能重新使用已经绑定过的接口
int reuse = 1;
if ( setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(int)) == -1)
error(" cant set the reuse option on the scoket");
//将服务器socket绑定到网络地址上
int c = bind(socket, (struct sockaddr*) &name, sizeof(name));
if (c == -1)
error("cant bind to socket");
}
int sent_msg_to_client(int socket, char *s)
{
int result = send(socket, s, strlen(s), 0);
if (result == -1)
fprintf(stderr, "%s : %s\n", "error talking to client", strerror(errno) );
return result;
}
int main(int argc, char const *argv[])
{
int server_sockfd;
int len;
server_sockfd = open_listener_socket();
bind_to_port(server_sockfd , 30000);
// 监听连接请求,监听队列长度为10
if (listen(server_sockfd , 10) == -1)
error("cant listen");
struct sockaddr_storage client_addr; //client_addr 保存连接客户端的详细信息
unsigned int address_size = sizeof(client_addr);
puts("waiting for connection");
char buf[255];
//等待客户端连接请求到达
int client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_addr, &address_size);
if (client_sockfd == -1)
error("accept error");
sent_msg_to_client( client_sockfd, "Welcome to my server\n>");
while ((len = recv(client_sockfd, buf, sizeof(buf), 0)) > 0)
{
buf[len] = ‘\0‘;
printf("%s\n", buf);
if (strncasecmp("Who is there?", buf, 12))
sent_msg_to_client(client_sockfd, "You should say ‘Who is there?‘ !\r\n>");
else
{
sent_msg_to_client(client_sockfd, "Oscar\r\n>");
while ((len = recv(client_sockfd, buf, sizeof(buf), 0)) > 0)
{
buf[len] = ‘\0‘;
printf("%s\n", buf);
if (strncasecmp("Oscar who?", buf, 9))
{
sent_msg_to_client(client_sockfd, "You should say ‘Oscar who?‘ !\r\n>");
continue;
}
else
{
sent_msg_to_client(client_sockfd, "Oscar silly question, you get a silly answer\r\n");
break;
}
}
}
}
return 0;
}
- 客户端程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <arpa/inet.h>
void error(char *msg)
{
fprintf(stderr, "%s : %s\n", msg, strerror(errno) );
exit(1);
}
int open_listener_socket(void)
{
int s = socket(AF_INET, SOCK_STREAM, 0); //创建服务器Socket , Ipv4协议,面向连接通信,TCP协议
if (s == -1)
error("scoket open error");
return s;
}
int main(int argc, char *argv[])
{
int client_sockfd;
char buf[255];
int len;
struct sockaddr_in client_addr; //客户端网络地址结构体
client_addr.sin_family=PF_INET; //设置为IP通信
client_addr.sin_addr.s_addr=inet_addr("127.0.0.1");//服务器IP地址
client_addr.sin_port=htons(30000); //服务器端口号
/*创建客户端套接字--IPv4协议,面向连接通信,TCP协议*/
client_sockfd= open_listener_socket();
/*将套接字绑定到客户端的网络地址上*/
if(connect(client_sockfd,(struct sockaddr *)&client_addr,sizeof(struct sockaddr)) == -1)
{
error("cant connected to server");
}
puts("connected to server\n");
len=recv(client_sockfd,buf,BUFSIZ,0);//接收服务器端信息
buf[len]=‘\0‘;
printf("%s",buf); //打印服务器端信息
while(1)
{
//scanf("%s",buf);
gets(buf);
if(!strcmp(buf,"quit"))
break;
len=send(client_sockfd,buf,strlen(buf),0);
len=recv(client_sockfd,buf,BUFSIZ,0);
buf[len]=‘\0‘;
printf("%s",buf);
}
close(client_sockfd);//关闭套接字
return 0;
}
时间: 2024-10-22 05:03:00