作者:--郑煦桥-- | 来源:互联网 | 2023-10-11 23:02
来源搞代码网《c++实现linuxsocket文件传输服务器》关键词:c++,linux,socket,linuxsocketc++实现linuxsocket文件传输服务器serv
来源编程笔记《c++++实现linux socket 文件传输服务器》http://www.gaodaima.com/68536.html
关键词:c++,linux,socket,linux socket
c++++实现linux socket 文件传输服务器
server:
int main()
{
logFd = open("server.log", O_WRONLY | O_CREAT | O_APPEND, S_IRWXU | S_IRWXG);
struct sockaddr_in server_addr;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
//inet_aton("10.192.67.148", &server_addr.sin_addr);
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
int portFd = 0;
char portBuf[128];
memset(portBuf,0,128);
if((portFd = open("port.conf", O_RDONLY, 0)) <0)
{
writeLog("open port.conf failed, check if the file exists");
exit(1);
}
if(read(portFd, portBuf, 1024) <0)
{
writeLog("read file failed");
}
server_addr.sin_port = htons(atoi(portBuf));
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if(server_socket <0)
{
writeLog("create socket failed");
return 1;
}
FileTransServer server(server_addr, server_socket);
server.StartServer();
close(logFd);
}