很久很久以前写的一个C/S程序(客户端与服务端聊天的网络程序)
与你的要求差不多
具体的不同可以做一些修改
服务端代码
#include "stdio.h"
#include "sys/types.h"
#include "stdlib.h"
#include "sys/socket.h"
#include "string.h"
#include "errno.h"
#include "netdb.h"
#include "netinet/in.h"
#include "arpa/inet.h"
#define PORT 8888
int get_socket(void)
{
int sockfd;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("Creat Socket Error:%s\n",strerror(errno));
exit(1);
}
else
return sockfd;
}
void init_data(struct sockaddr_in *server_addr)
{
bzero(server_addr,sizeof(struct sockaddr_in));
server_addr->sin_family=AF_INET;
server_addr->sin_addr.s_addr=htonl(INADDR_ANY);
server_addr->sin_port=htons(PORT);
}
void creat_bind(int *sockfd,struct sockaddr_in *server_addr)
{
if(bind(*sockfd,(struct sockaddr *)server_addr,sizeof(struct sockaddr))==-1)
{
printf("Creat Bind Error:%s\n",strerror(errno));
exit(1);
}
}
void creat_listen(int *sockfd)
{
if(listen(*sockfd,SOMAXCONN)==-1)
{
printf("Listen Error:%s\n",strerror(errno));
exit(1);
}
}
int creat_accpet(int sockfd,struct sockaddr_in *client_addr)
{
int new_fd;
int sin_size=sizeof(struct sockaddr_in);
if((new_fd=accept(sockfd,(struct sockaddr *)client_addr,&sin_size))==-1)
{
printf("Accept Error:%s\n",strerror(errno));
exit(1);
}
else
return new_fd;
}
int main(void)
{
int sockfd,new_fd;
struct sockaddr_in server_addr,client_addr;
char buffer[1024];
sockfd=get_socket();
init_data(&server_addr);
creat_bind(&sockfd,&server_addr);
creat_listen(&sockfd);
new_fd=creat_accpet(sockfd,&client_addr);
while(1)
{
int n;
n=read(new_fd,buffer,sizeof(buffer));
buffer[n]='\0';
printf("Form:%s\n:%s\n",inet_ntoa(client_addr.sin_addr),buffer);
bzero(buffer,sizeof(buffer));
printf("Please Enter msg:");
n=0;
while((buffer[n++]=getchar())!='\n');
write(new_fd,buffer,sizeof(buffer));
}
close(new_fd);
close(sockfd);
}
客户端代码
#include "stdio.h"
#include "sys/socket.h"
#include "netdb.h"
#include "netinet/in.h"
#include "string.h"
#include "errno.h"
#include "stdlib.h"
#include "sys/types.h"
void error_get(int argc)
{
if(argc!=3)
{
printf("Error:%s\n",strerror(errno));
exit(1);
}
}
struct hostent *get_host(char *hostname)
{
struct hostent *host;
host=(struct hostent *)malloc(sizeof(struct hostent));
if((host=gethostbyname(hostname))==NULL)
{
printf("Get Hostname Error:%s\n",strerror(errno));
exit(1);
}
else
return host;
}
int get_port(char *port)
{
return atoi(port);
}
int creat_socket(void)
{
int sockfd;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("Creat Socket Error:%s\n",strerror(errno));
exit(1);
}
else
return sockfd;
}
void init_data(struct sockaddr_in *server_addr,int portnumber,struct hostent *host)
{
bzero(server_addr,sizeof(struct sockaddr_in));
server_addr->sin_family=AF_INET;
server_addr->sin_port=htons(portnumber);
server_addr->sin_addr=*((struct in_addr *)host->h_addr);
}
void creat_connect(int sockfd,struct sockaddr_in *server_addr)
{
printf("开始连接. . .");
if(connect(sockfd,(struct sockaddr *)server_addr,sizeof(struct sockaddr))==-1)
{
printf("Creat Connect Error:%s\n",strerror(errno));
exit(1);
}
printf("\n连接成功!\n");
}
int main(int argc,char **argv)
{
int portnumber,sockfd;
struct hostent *hostname;
struct sockaddr_in server_addr;
char buff[1024];
int n;
error_get(argc);
hostname=get_host(argv[1]);
portnumber=get_port(argv[2]);
sockfd=creat_socket();
init_data(&server_addr,portnumber,hostname);
creat_connect(sockfd,&server_addr);
while(1)
{
int i=0;
printf("Please Enter msg:");
while((buff[i++]=getchar())!='\n');
write(sockfd,buff,sizeof(buff));
bzero(buff,sizeof(buff));
n=read(sockfd,buff,sizeof(buff));
buff[n]='\0';
printf("From:%s\n:%s\n",argv[1],buff);
}
close(sockfd);
return 0;
}
不就是写个web吗
这怎么了?设计啊!哪不会啊!?