一些程序代码备忘录!

 

可以生成手机号码的程序,并写入Txt文件.

 

#include <iostream> 

#include <windows.h>

using namespace std; 

int main() 

{    

	FILE *pFile;    

	pFile=fopen("phone.txt","a");     

	char buf[128];     

	memset(buf,0,sizeof(buf));  

	srand( GetTickCount());    

	int number;   

	cout <<"请输入需要手机号码的数量:";    

	cin >> number;      char num[128];   

	memset(num,0,sizeof(num));    

	int temp; 

    cout <<"请输入需要手机号码的开头:";   

	cin>>temp;     itoa(temp,num,10);  

	for(int i=0;i<number;i++)   

	{        

		fwrite(num,1,strlen(num),pFile);         

		for(int x =3;x <=10;x++)        

		{      

			itoa(rand()%10,buf,10);     

			fwrite(buf,1,strlen(buf),pFile);  

		}           

		fwrite(" ",1,1,pFile);    

		fflush(pFile);     

    }    

	cout <<"号码生成完毕!"<<endl;    

	getchar();    

	return 0;

}

 一个服务器带日志的程序代码:

#include <stdio.h> 

#include <string.h>

#include <time.h>

int main() 

{    

	FILE *pFile;  

	char timebuf[128];    

	char Fnamebuf[128];  

	memset(Fnamebuf,0,sizeof(Fnamebuf)); 

	memset(timebuf,0,sizeof(timebuf));       

	time_t stime;     time(&stime);  

	struct tm *today;    

	char buf[128];   

	memset(buf,0,sizeof(buf));    

	today = localtime(&stime);  

	strftime(buf,sizeof(buf),"%Y-%m-%d",today);  

	strcat(buf,".log");  

	pFile=fopen(buf,"a");    

	strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",today);   

	strcat(buf," 服务器启动 /n"); 

	fwrite(buf,1,strlen(buf),pFile);       

	fflush(pFile);    

	fclose(pFile);    

	return 0; 

} 

 

select模型 + udp 服务端

 

#include <iostream> 

#include <stdio.h> 

#include <Winsock2.h> 

using namespace std;



SOCKET m_serversocket;

char buf[256];   //选择套接字版本 



void Loadsocket() 

{     

	WORD wVersionRequested;   

	WSADATA wsaData;    

	int err;     //请求winsock版本(1,1)   

	wVersionRequested = MAKEWORD(1,1);   

	err = WSAStartup(wVersionRequested,&wsaData); 

	if(err != 0 )     

	{        

		return;    

	}    

	//检查,调用winsock版本号,如果不是则取消  

	if(LOBYTE(wsaData.wVersion) != 1 ||  HIBYTE(wsaData.wVersion) != 1)   

	{      

		WSACleanup();   

		return;   

	}

}  



//接受数据 

void RecvData() 

{    

	//设置select等待时间值  

	struct timeval timeout;     

	timeout.tv_sec = 5;    

	timeout.tv_usec=0;         

	//服务轮询    

	while(1)   

	{      

		fd_set fdsocket;   

		FD_ZERO(&fdsocket);    

		FD_SET(m_serversocket,&fdsocket);    

		if(select(NULL,&fdsocket,NULL,NULL,(timeval *)&timeout))   //等待事件到来 

		{       

			if(FD_ISSET(m_serversocket,&fdsocket))      //事件可以读             

			{          

				sockaddr_in sockclient;   

				int len = sizeof(sockclient);   

				//接受数据              

				recvfrom(m_serversocket,buf,sizeof(buf),0,(sockaddr*)&sockclient,&len);      

				printf("%s:%s ",inet_ntoa(sockclient.sin_addr),buf);       

			}      

		} 

	} 

}  

int main() 

{     

	//选择socket库版本    

	Loadsocket();      

	//选择协议    

	m_serversocket=socket(AF_INET,SOCK_DGRAM,0);       

	//对套接字初始化   

	sockaddr_in socksrv;   

	socksrv.sin_family=AF_INET;   

	socksrv.sin_port=htons(5800);     

	socksrv.sin_addr.S_un.S_addr=INADDR_ANY;      

	//绑定地址到套接字    

	bind(m_serversocket,(SOCKADDR *)&socksrv,sizeof(socksrv));   

	//接受数据   

	RecvData();    

	return 0; 

} 

 

08.6.10  windows 类成员线程

#include <iostream>
  
#include <windows.h>
  
using namespace std;

class A 
{ 
public: 
	static unsigned long _stdcall Thread(void *param);
	bool StartThread(); 
}; 

//类的线程函数
unsigned long _stdcall A::Thread(void *param) 
{ 
	printf("taskmain start!/n");
	return true; 
} 
 
bool A::StartThread()
{
	printf("StartTask start!/n");
	CreateThread(NULL,0,Thread,NULL,0,NULL);
	Sleep(1);
	return 0;
} 


int main()
{
	A a;
	a.StartThread();
	return 0;
}
 
/////Linux 类成员线程函数////////////
///08.06.10///////////////////

#include 
   
#include 
   
using namespace std;

class A
{
public:
        void StartThread();
        static  void *Thread(void *param);
};

void A::StartThread()
{
        printf("startThread!");
        pthread_t tid;
        int err;
        err = pthread_create(&tid,NULL,Thread,NULL);
        sleep(2);
}

void *A::Thread(void *param)
{
        printf("Thread!/n");
}

int main()
{
        A a;
        a.StartThread();
        return 0;
}
 

你可能感兴趣的:(一些程序代码备忘录!)