Linux下Socket编程简单例子

#include#include#include#include#include#incl网络
强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <time.h>
#define SERVPORT 3333 
#define BACKLOG 10 
main()
{
 int sockfd,client_fd ,sin_size; 
struct sockaddr_in my_addr; 
struct sockaddr_in remote_addr; 
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
{
 perror("socket error"); exit(1);
 }
 my_addr.sin_family=AF_INET;
 my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
 bzero(&(my_addr.sin_zero),8);
 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) 
{
 perror("bind error");
 exit(1);
 }
 if (listen(sockfd, BACKLOG) == -1) 
{
 perror("listen error");
 exit(1);
 }
 while(1) {
 sin_size = sizeof(struct sockaddr_in);
 if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1) 
{
 perror("accept error");
 continue;
}
 printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));
if (!fork()) 
{
time_t timep;
time (&timep);
char temp[100]="Hello,you are connected!the time now is:";
char * t=ctime(&timep);
strcat(temp,t);
 if (send(client_fd, temp, 100, 0) == -1)
 perror("send error");
close(client_fd);
exit(0);
}
 close(client_fd);
 }
 }


说明:服务器上要先起这个服务,然后客户端就可以访问了。可以用telnet   你的服务器IP  3333(端口)

 

修改版:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <time.h>
#define SERVPORT 3333 
#define BACKLOG 10 
main()
{
 int sockfd,client_fd ,sin_size; 
struct sockaddr_in my_addr; 
struct sockaddr_in remote_addr; 
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
{
 perror("socket error"); exit(1);
 }
 my_addr.sin_family=AF_INET;
 my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
 bzero(&(my_addr.sin_zero),8);
 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) 
{
 perror("bind error");
 exit(1);
 }
 if (listen(sockfd, BACKLOG) == -1) 
{
 perror("listen error");
 exit(1);
 }
 while(1) {
 sin_size = sizeof(struct sockaddr_in);
 if ((client_fd = accept(sockfd, (struct sockaddr *)&remote_addr, &sin_size)) == -1) 
{
 perror("accept error");
 continue;
}
 printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));
if (!fork()) 
{
time_t timep;
time (&timep);
char temp[1024]="\r\n\r\n<html><head><title>你好</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body>当前系统时间:";
char * t=ctime(&timep);
strcat(temp,t);
strcat(temp,"</body></html>");
send(client_fd,"HTTP/1.1 200 OK\r\nServer:HWS\r\nContent-Length:1024\r\nContent-Type:text/html;charset=utf-8\r\n", 90, 0);
send(client_fd, temp, 1024, 0);

close(client_fd);

exit(0);
}
 close(client_fd);
 }
 }


 

本文来源21aspnet,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/2287

发表评论