当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » C/C++  » Windows编程
分享到: 
收藏 +0
2
单文件 无依赖库  文件存为s1.cpp
windows下 g++ s1.cpp -lwsock32即可得到生成的a.exe
只有socket用了win32 socket 多线程用了win32 createThread
其他的都是c++标准的
随手写的应付实验  渣的不行 
第一次写win32的东西  
我把代码拆分加了一个图形界面的qt版本带有启动按钮关闭按钮的版本的 也是应急版本 很渣的
大家要可以私信我
标签: Windows C++ Sockets

代码片段(1) [全屏查看所有代码]

1. [代码]s1.cpp     跳至 [1] [全屏预览]

#include <iostream>
#include <Winsock.h>
#include <stdio.h>
//#include <WinSock2.h>
#pragma comment(lib, "WSOCK32.LIB") 
#define ReportWSAError(s) printf("Error #%d in %s./n", WSAGetLastError(), #s) 
#include <Windows.h>
#include <string>
#include <string.h>
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;

/*支持7种文件方式
txt   1
html  2
json  3
jpg   4
gif   5
png   6
zip   7
pdf   8
mp3   9
mp4   10
*/
int getType(string );
DWORD WINAPI worker_thread(LPVOID );

int main(int argc,char* argv[])
{
	WSADATA wsd;
    string base_dir="D:/tmp/";
	WSAStartup(MAKEWORD(2,2),&wsd);
	SOCKET m_server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);  
	sockaddr_in serveraddr;
	serveraddr.sin_family= AF_INET;
	serveraddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
	//绑定地址
	serveraddr.sin_port=htons(85);
	//绑定端口
	if(bind(m_server, (LPSOCKADDR)&serveraddr, sizeof(serveraddr)) == SOCKET_ERROR)	
    {
        ReportWSAError(bind());
        WSACleanup();
        return 1;
    }
    if(listen(m_server, 5) == SOCKET_ERROR)
    {
        ReportWSAError(listen());
        WSACleanup(); 
        return 1;
    }
    
	while(TRUE) // Listening circulation
    {
        int nAddrLen = sizeof(sockaddr_in);
        sockaddr_in remoteAddr;
        struct ThreadParameter{
                sockaddr_in remoteAddr;
                SOCKET  remoteSocket;
                string base_dir;
        } tp;
        tp.base_dir="D:/tmp/";
        tp.remoteSocket=accept(m_server, (SOCKADDR*)&(tp.remoteAddr), &nAddrLen); 
        HANDLE handle = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE)worker_thread,&tp, 0, NULL);
        //WaitForSingleObject(handle, INFINITE); 
    }
	return 0;
}

int getType(string filename)
{
    string f1=filename;
    char *p;
    p=strstr(f1.c_str(),".");
    string b1=p;
    string b2=b1.substr(1,b1.length());
   if(strcmp("txt",b2.c_str())==0)
        return 1;
    else if(strcmp("html",b2.c_str())==0)
        return 2;
    else if(strcmp("json",b2.c_str())==0)
        return 3;
    else if(strcmp("jpg",b2.c_str())==0)
        return 4;
    else if(strcmp("gif",b2.c_str())==0)
        return 5;
    else if(strcmp("png",b2.c_str())==0)
        return 6;
    else if(strcmp("zip",b2.c_str())==0)
        return 7;
    else if(strcmp("pdf",b2.c_str())==0)
        return 8;
    else if(strcmp("mp3",b2.c_str())==0)
        return 9;
    else if(strcmp("mp4",b2.c_str())==0)
        return 10;
}

DWORD WINAPI worker_thread(LPVOID pParam)
{
   struct ThreadParameter{
                sockaddr_in remoteAddr;
                SOCKET  remoteSocket;
                string base_dir;
    };
    ThreadParameter *tp =(ThreadParameter *)pParam;
    SOCKET remoteSocket=tp->remoteSocket;
    sockaddr_in remoteAddr=tp->remoteAddr;
    string base_dir=tp->base_dir;
    char buffer[1024];
    int byteRecv;
    if(remoteSocket == SOCKET_ERROR)            
    {
            ReportWSAError(accept());
    }
        byteRecv = recv(remoteSocket, buffer, 1023, 0);
        buffer[byteRecv] = 0;
        printf("%s\n", buffer);
        stringstream ss1;
        ss1<<buffer;
        char bf1[100];
        string filename;
        while ( ss1.getline(bf1, sizeof(buffer)) )
        {
             char *p;
             p=strstr(bf1,"GET");
             //找到含有GET的那一行
             if(p!=0)
             {
                   string sp1=p;
                   string sp2=sp1.substr(4,sp1.length()-13);
                   string sp3=sp2.substr(1,sp2.length());
                   //sp3即所获取的文件的名字
                   if(sp3.length()==1)
                   {
                      sp3="index.html ";
                   }
                   filename=base_dir+sp3;
                   filename=filename.substr(0,filename.length()-1);
             }
        }
         //下面是获取文件的内容输出出去
        int type=getType(filename);
        //type为1,2,3为纯文本  plain,html,json   type不为1为二进制流文件,这里仅仅处理图像
        //这里仅仅支持6种类型
        if(type==1 || type==2 || type==3)
        {     
             ifstream in(filename.c_str());
             if(!in)
             {
                cerr << "打开文件失败";
             }
             const char * header;
             switch(type){
                case 1:
                    header = "HTTP/1.1 200 OK\r\nContent-Type: plain/text\r\nConnection: close\r\n\r\n";
                    break;
                case 2:
                    header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";
                    break;
                case 3:
                    header = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n";
                    break;
                default:
                     header = "HTTP/1.1 200 OK\r\nContent-Type: plain/text\r\nConnection: close\r\n\r\n";
                     break;
             }
             send(remoteSocket, header, strlen(header), 0);
             string get_neirong;
             while(getline(in, get_neirong))
             {
               
                send(remoteSocket, get_neirong.c_str(), get_neirong.length(), 0);
             }
        }
        else
        {
            ifstream in(filename.c_str(),ios::binary);
            if(!in)
            {
                //cerr << "打开文件失败";
                
             }
            const char *header = "HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\nConnection: close\r\n\r\n";
            send(remoteSocket, header, strlen(header), 0);
            while(!in.eof())
            {
                
                char c[1024];
                in.read(c, sizeof(char) * 1024);  
                send(remoteSocket, c, sizeof(char) * 1024, 0);
            } 
        }
        printf("this is the remoteAddr %s \n", inet_ntoa(remoteAddr.sin_addr));     
        closesocket(remoteSocket);
}


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(2)

  • 1楼:hahanihao 发表于 2016-06-13 11:07 回复此评论
    骗子,哪里有select
  • 2楼:hahanihao 发表于 2016-06-13 11:08 回复此评论
    瞎子点错连接了