#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORTNUM 6789
#define MAX_SIZE 1024
int main(int argc, char *argv[])
{
int sockfd,new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size;
char msg[MAX_SIZE] = "Hello! ...\n";
//未做异常判断 socket() bind() listen() accept() wirte()错误返回值为-1
sockfd=socket(AF_INET,SOCK_STREAM,0);
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_addr.s_addr=inet_addr("127.0.0.1");
server_addr.sin_port=htons(PORTNUM);
int ret = bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr));
ret = listen(sockfd,5);
while(1)
{
sin_size=sizeof(struct sockaddr_in);
new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size);
fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));
ret = write(new_fd,msg, strlen(msg));
while(1)
{
bzero(msg, MAX_SIZE);
fgets(msg, MAX_SIZE, stdin);
msg[strlen(msg) - 1] = '\0';
printf("inputed: %s----input \"exit\" to quit\n", msg);
if(strcmp(msg, "exit") == 0) break;
ret = write(new_fd, msg, strlen(msg));
}
close(new_fd);
}
close(sockfd);
exit(0);
}
——客户端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORTNUM 6789
int main(int argc, char *argv[])
{
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int nbytes;
host=gethostbyname(argv[1]);//返回值不为NULL时正确
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET; // IPV4
server_addr.sin_port=htons(PORTNUM); //
server_addr.sin_addr=*((struct in_addr *)host->h_addr); // IP地址
int ret = connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr));
while((nbytes = read(sockfd, buffer, 1024)) != -1)
{
if(nbytes > 0)
{
buffer[nbytes]='\0';
printf("client received:%s\n",buffer);
bzero(buffer, 1024);
}
}
close(sockfd);
exit(0);
}
基于UDP的socket 服务端和客户端实现
——服务端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 7890
#define MAX_MSG_SIZE 1024
int main(void)
{
int sockfd;
struct sockaddr_in addr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&addr,sizeof(struct sockaddr_in)); // 初始化,置0
addr.sin_family=AF_INET; // Internet
addr.sin_addr.s_addr=htonl(INADDR_ANY);
//addr.sin_addr.s_addr=inet_addr("127.0.0.1");
addr.sin_port=htons(SERVER_PORT);
int ret = bind(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in));
struct sockaddr_in client_addr;
int addrlen,n;
char msg[MAX_MSG_SIZE];
while(1)
{
bzero(msg,sizeof(msg)); // 初始化,清零
addrlen = sizeof(struct sockaddr);
n=recvfrom(sockfd,msg,MAX_MSG_SIZE,0,(struct sockaddr*)&client_addr,&addrlen); // 从客户端接收消息
msg[n]=0;
fprintf(stdout,"Server received %s",msg); // 显示消息
}
close(sockfd);
}
——客户端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 7890
#define MAX_BUF_SIZE 1024
int main(int argc,char **argv)
{
int sockfd;
struct sockaddr_in addr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&addr,sizeof(struct sockaddr_in)); // 初始化,置0
addr.sin_family=AF_INET; // Internet
addr.sin_port=htons(SERVER_PORT);
int ret = inet_aton(argv[1],&addr.sin_addr);
char buffer[MAX_BUF_SIZE];
int n;
while(1)
{
fgets(buffer,MAX_BUF_SIZE,stdin);
struct sockadd_int *t_addr = &addr;
sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr*)t_addr,sizeof(struct sockaddr_in));
bzero(buffer,MAX_BUF_SIZE);
}
close(sockfd);
}