#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define SERVER_PORT 8080
int main()
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
perror("socket error\n");
exit(-1);
}
printf("sockfd is %d\n", sockfd);
/* 设置端口复用 */
int optVal = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optVal, sizeof(optVal));
struct sockaddr_in localAddress;
/* 清楚脏数据 */
memset(&localAddress, 0, sizeof(localAddress));
localAddress.sin_family = AF_INET; //IPv4
localAddress.sin_port = htons(SERVER_PORT);
localAddress.sin_addr.s_addr = INADDR_ANY;
socklen_t localAddressLen = sizeof(localAddress);
/* 绑定端口和IP */
int ret = bind(sockfd, (const struct sockaddr*)&localAddress, localAddressLen);
if (ret == -1)
{
perror("bind err\n");
exit(-1);
}
/* 监听 */
ret = listen(sockfd, 10);
if (ret == -1)
{
perror("listen error\n");
exit(-1);
}
/* */
struct sockaddr_in clientAddress;
memset(&clientAddress, 0, sizeof(clientAddress));
socklen_t clientAddressLen = 0;
/* accept的返回值是和【客户端】通信的文件描述符 */
int connfd = accept(sockfd, (struct sockaddr*)&clientAddress, &clientAddressLen);
if (connfd == -1)
{
perror("accept error\n");
exit(-1);
}
printf("connfd is %d\n", connfd);
char buffer[32] = { 0 };
strcpy(buffer, "偶像\n");
write(connfd, buffer, sizeof(buffer) - 1);
/* 回收资源 */
close(connfd);
close(sockfd);
return 0;
}