C++编写的socket服务端程序!

// *******************************************************************
// server.cpp : 服务器端程序
// *******************************************************************
#include <StdAfx.h>
#include <winsock2.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <fcntl.h>
#include <io.h>
#include <sys\stat.h>
#define PORT_SERVER 8000
#define NUM_CLIENTS 10
using namespace std;




int WriteFile(char *s);//写入文件函数
int Parse(int Lenth,char *s1);//解析前两个字符函数
int GetFileContent(int Lenth2,int Lenth3,char *s3);//获取文件名和文件内容函数
int CreateFile(char *s4,char *s5);//创建文件
int main(int argc, char* argv[])
{


std::cout<<"###########################"<<std::endl;
std::cout << "#socket服务端启动.........#" << std::endl;
std::cout<<"###########################"<<std::endl;


//初始化winsock 
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 0);
if (0 != WSAStartup(sockVersion, &wsaData))
{
std::cout << "Failed to retrive socket version."<< std::endl;
return 0;
}
SOCKET sock_sev;
sock_sev = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == sock_sev)
{
std::cout << "Invalid socket." << std::endl;
WSACleanup();
return 0;
}
sockaddr_in addr_sev;
addr_sev.sin_family = AF_INET;
addr_sev.sin_port = htons(PORT_SERVER);
addr_sev.sin_addr.s_addr = INADDR_ANY;
if (SOCKET_ERROR == bind(sock_sev, (sockaddr *)&addr_sev, sizeof(addr_sev)))
{
std::cout << "Failed to bind." << std::endl;
WSACleanup();
return 0;
}
if (SOCKET_ERROR == listen(sock_sev, NUM_CLIENTS))
{
std::cout << "Failed to listen." << std::endl;
WSACleanup();
return 0;
}
sockaddr_in addr_client;
int nAddrLen = sizeof(addr_client);


while (true)
{
char buf[1024];
int recv_size = 0;
   
SOCKET sock_client;
char tmp[512];
sock_client = accept(sock_sev, (sockaddr *)&addr_client, &nAddrLen);
   
if (INVALID_SOCKET == sock_client)
{
std::cout << "Failed to accept." << std::endl;
continue;
}
sprintf(tmp, "Your IP is %s\n", inet_ntoa(addr_client.sin_addr));
send(sock_client, tmp, strlen(tmp), 0);
std::cout << "Connection from " << inet_ntoa(addr_client.sin_addr) << std::endl;


//接收字符串
recv_size = recv(sock_client,buf,sizeof(buf),2);
Sleep(250);
if ( 1024 > recv_size )
{
buf[recv_size] = '\0';
std::cout << "接收的字符串是:\n" << buf << std::endl;
Parse(recv_size,buf);
WriteFile(buf);
//buf[0] = '\0';
memset(buf, 0, sizeof(buf));
}else
{
std::cout << "接收的字符串溢出:" << std::endl;
buf[0] = '\0';
   memset(buf, 0, sizeof(buf));

closesocket(sock_client);
}
//system("PAUSE");
WSACleanup();
return 0;
}
//写入文件函数
int WriteFile(char *buf)
{
FILE *fp;
    if ((fp = fopen("server.txt", "wb"))== NULL)
    {
        fprintf(stderr,"不能打开文件");
        return 1;
    }else if (*buf == NULL) 
{
std::cout << "接收字符串为空,写入本地文件失败!\n" << std::endl;
}else
{
fwrite(buf,strlen(buf)+1,1,fp);
std::cout << "另存为本地'server.txt'文件成功!\n" << std::endl;
fclose(fp);
}
    return 0;
}
//解析前两个字符函数
int Parse(int Lenth,char *s1)
{
unsigned int FileLen;
unsigned int Oper;
unsigned int num[2];
string Str;
string fileName;
char FileName[20];


Str = s1;
//截取第一个、第二个字符
for (int i = 0; i < 2; i++) 
{
num[i] = (int)s1[i] - 48;
}
FileLen = num[0];
Oper = num[1];
if (FileLen > 0 ) 
{
cout<<"文件名长度:"<<FileLen<<endl;
}else if (FileLen == NULL) 
{
cout<<"接收的文件名为空!"<<endl;
}else
{
cout<<"解析字符有误!"<<endl;
}

if (4 > Oper > 0 && Oper == 1) {
cout<<"操作符是:"<<Oper<<",创建文件并写入内容开始!"<<endl;
GetFileContent(FileLen,Lenth,s1);
cout<<"创建文件并写入内容成功"<<endl;
}else if (4 > Oper > 0 && Oper == 2) 
{
cout<<"操作符是:"<<Oper<<",修改文件内容"<<endl;
GetFileContent(FileLen,Lenth,s1);
cout<<"修改文件内容成功"<<endl;
}else if (4> Oper > 0 && Oper == 3) 
{
cout<<"操作符是:"<<Oper<<",删除文件"<<endl;
fileName = Str.substr(2,FileLen);
strcpy(FileName, fileName.c_str());
cout<<"删除文件"<<endl;
remove(FileName);
cout<<"删除文件成功"<<endl;
//system("del FileName");
}else
{
cout<<"操作符有误!"<<endl;
}
return 0;
}
//获取文件名和文件内容函数
int GetFileContent(int Lenth1,int Lenth2,char *s3)
{
string Content;
string Str;
string fileName;
char FileName[20];
char FileContent[1000];
FILE *fp;
    string fileContent;
fstream file;

Str=s3;
_fmode = O_BINARY;
Content = s3;
//截取文件名
fileName = Str.substr(2,Lenth1);
strcpy(FileName, fileName.c_str());
cout<<"文件名是:"<<FileName<<endl;
//截取文件内容
fileContent= Content.substr(Lenth1+2,Lenth2);
strcpy(FileContent, fileContent.c_str());
cout<<"文件内容是:"<<FileContent<<endl;
//调用创建文件函数
CreateFile(FileName,FileContent);
return 0;
}
//创建文件
int CreateFile(char *s4,char *s5)
{
int handle;
//创建文件
handle = creat(s4, S_IREAD | S_IWRITE);//s4(文件名)
cout<<"创建"<<s4<<"文件成功"<<endl;
//写内容到文件
write(handle,s5,strlen(s5));//s5(文件内容)
cout<<"写入"<<s4<<"文件内容成功"<<endl;
//关闭文件
close(handle);
return 0;
}

没有更多推荐了,返回首页