c语言服务
① c语言如何写成服务来启动
注意包含文件要有
#include <stdio.h>
#include <windows.h>
② 如何用C语言来编写让系统中某个服务停止或重启
在Windows下,可以使用一系列API来完成该功能。
首先,调用OpenSCManager来获得服务管理器句柄。
然后,通过服务器管理器句柄,调用OpenService来打开指定服务名称的服务句柄。
通过服务句柄,调用ControlService来进行你想要的控制,比如暂停,停止,重启等操作。
最后,记得调用CloseServiceHandle来关闭上述句柄,以释放内核资源。
如下是我从以前的的一段代码中COPY过来的一点示例,该函数是准备删除一个服务,在删除服务之前,停止该服务。
BOOL Uninstall()
{
if ( !IsInstalled() )
return TRUE;
SC_HANDLE hSCM = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if ( hSCM == NULL )
{
//MessageBox( NULL, _T( "打开服务管理器失败!" ), szServiceName, MB_OK );
return FALSE;
}
SC_HANDLE hService = OpenService( hSCM, szServiceName, SERVICE_STOP | DELETE );
if ( hService == NULL )
{
CloseServiceHandle( hSCM );
MessageBox( NULL, _T( "服务不存在!" ), szServiceName, MB_OK );
return FALSE;
}
SERVICE_STATUS status;
ControlService( hService, SERVICE_CONTROL_STOP, &status );
BOOL bDelete = DeleteService( hService );
CloseServiceHandle( hService );
CloseServiceHandle( hSCM );
if ( bDelete )
{
MessageBox( NULL, _T( "删除服务成功!" ), szServiceName, MB_OK );
return TRUE;
}
MessageBox( NULL, _T( "删除服务失败!" ), szServiceName, MB_OK );
//LogEvent(_T("Service could not be deleted"));
return FALSE;
}
③ c语言访问服务器
lz要先知道什么是socket,它是TCP/IP协议的API。再上层是http udp之类传输报文协议。而什么是服务器,如你所说tomcat服务器,他是一个http(s)服务器。处理由客户发送的HTTP报文。并返回报文给客户。
简单来说,http就是socket的一个封装。所以c语言使用socket理所当然能访问任何服务器。至于使用什么格式,你可以看看HTTP报文格式。
④ 先来先服务算法(C语言版)
#include<stdio.h>
#include<stdlib.h>
typedef struct process_FCFS{
float arrivetime;//到达时间
float servetime;//服务时间
float finishtime;//完成时间
float roundtime;//周转时间
float daiquantime;//带权周转时间
struct process_FCFS *link;//结构体指针
}FCFS;
FCFS *p,*q,*head=NULL;
struct process_FCFS a[100];
//按到达时间进行冒泡排序
struct process_FCFS *sortarrivetime(struct process_FCFS a[],int n)
{
int i,j;
struct process_FCFS t;
int flag;
for(i=1;i<n;i++)
{
flag=0;
for(j=0;j<n-i;j++)
{
if(a[j].arrivetime>a[j+1].arrivetime)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
flag=1;//交换
}
}
if(flag==0)//如果一趟排序中没发生任何交换,则排序结束
break;
}
return a;
}
//先来先服务算法
void print(struct process_FCFS a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("到达时间:%f",a[i].arrivetime);
printf("服务时间:%f",a[i].servetime);
printf("完成时间:%f",a[i].finishtime);
printf("周转时间:%f",a[i].roundtime);
printf("带权周转时间:%f",a[i].daiquantime);
printf("\n");
}
}
void Fcfs(struct process_FCFS a[],int n)
{
int i;
a[0].finishtime=a[0].arrivetime+a[0].servetime;
a[0].roundtime=a[0].finishtime+a[0].arrivetime;
a[0].daiquantime=a[0].roundtime/a[0].servetime;
for(i=0;i<n;i++)
{
if(a[i].arrivetime<a[i-1].finishtime)
{
a[i].finishtime=a[i-1].finishtime+a[i].servetime;
a[i].roundtime=a[i].finishtime-a[i].arrivetime;
a[i].daiquantime=a[i].roundtime/a[i].servetime;
}
else
{
a[i].finishtime=a[i].arrivetime+a[i].servetime;
a[i].roundtime=a[i].finishtime-a[i].arrivetime;
a[i].daiquantime=a[i].roundtime/a[i].servetime;
}
}
printf("先来先服务\n");
print(a,n);
}
//主函数
void main()
{
int n,i;
printf("请输入有几个进程\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("arrivetime");
scanf("%f",&a[i].arrivetime);
printf("servetime");
scanf("%f",&a[i].servetime);
}
Fcfs(a,n);
}
⑤ 怎么用c语言加一个判断用户名服务的源程序
任务不是很清楚,你想说是用户名是否存在吗?请参考下面程序
/*Input : username*/
/*Output: NULL*/
/*Return
TRUE -----exist
FALSE-----not exist
*/
int checkUser(char *username)
{
if (username == NULL) {
return FALSE;
}
/*find one entry in your database for user*/
userEntry = GetFirstEntryFromDatabase();
while( userEntry!=NULL) {
/*Compare the name with entry's recording name*/
if (strcmp(userEntry->name, username) ==0) {
return TRUE;
}
userEntry = GetNextEntryFromDatabase();
}
return FALSE;
}
⑥ 寻找写代码大神,c语言,有偿服务
我这里有一份完整的学生信息管理系统源代码,限于知道篇幅,这里先粘贴出定义声明部分代码,以及运行结果截图。
详细内容私信联系~~
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#definestatusint
#defineNAME30/*各类名称字数上限*/
#defineSTU100/*学生人数上限*/
#defineSUB10/*课程数上限*/
#definePASS60/*及格分数下限(含)*/
#defineLA85/*A级别分数下限(含)*/
#defineLB70/*B级别分数下限(含)*/
#defineLC60/*C级别分数下限(含)*/
#defineLD0/*D级别分数下限(含)*/
typedefstructrecord{
floatscore;/*成绩分数*/
charlevel;/*成绩分数段*/
}record;
typedefstructstudent{
intid;/*id*/
intno;/*学号*/
charname[NAME];/*姓名*/
recordscores[SUB];/*各门课成绩分数*/
floattotalScore;/*总分*/
floataverageScore;/*平均分*/
chartotalLevel;/*总分数段*/
intranking;/*名次*/
}student;
charsubject[SUB][NAME];/*课程名称*/
/**********************以下为函数声明**********************/
statusinitProgram(studentstu[],int*stuNum,int*subNum,intmode);/*初始化,含测试、程序写定课程、自定义课程3种模式*/
voidinitResult(studentstu[],int*stuNum,int*subNum,intmode);/*判断初始化结果*/
statusinputTestStuInfo(studentstu[],int*stuNum,int*subNum);/*测试数据*/
statusdefSubject(int*subNum,charsubject[SUB][NAME]);/*自定义各门课程*/
intstuInfoEmpty(studentstu[],intstuNum);/*学生信息判空*/
statusstuInfoEmptyOp(studentstu[],intstuNum);/*学生为空时的处理*/
intnoRepeated(studentstu[],intstuNum,intno);/*学号判重*/
statusprintInputStuInfoheader(intsubNum);/*输出录入学生信息之表头*/
statusinputStuInfo(studentstu[],int*stuNum,intsubNum,intstuIndex,intismod);/*录入单个学生信息*/
statusinputAllStuInfo(studentstu[],int*stuNum,intsubNum);/*录入全部学生信息*/
chargetScoreLevel(floatscore);/*计算分数段*/
statuscalcStuInfo(studentstu[],intstuIndex,intsubNum);/*计算处理单个学生信息*/
statuscalcAllStuInfo(studentstu[],intstuNum,intsubNum);/*计算处理全部学生信息*/
statusprintStuInfoheader(intsubNum,intinclRanking);/*输出学生信息之表头*/
statusprintStuInfo(studentstu[],intstuIndex,intsubNum,intinclRanking);/*输出单个学生信息*/
statusprintAllStuInfo(studentstu[],intstuNum,intsubNum,intinclRanking);/*输出全部学生信息*/
intcompareStuInfo(studentstu[],intstuNum,intrsIndex[],intinclNo,intno,intinclName,charname[]);/*按学号、姓名查找学生信息,返回结果个数*/
statusfindStuInfo(studentstu[],intstuNum,intsubNum);/*查找符合条件的学生信息并输出*/
statusdeleteStuInfoByIndex(studentstu[],int*stuNum,intsubNum,intstuIndex);/*删除指定位置的学生信息*/
statusdelStuInfo(studentstu[],int*stuNum,intsubNum);/*删除学生信息*/
statusmodifyStuInfoByIndex(studentstu[],int*stuNum,intsubNum,intstuIndex);/*修改指定位置的学生信息*/
statusmodStuInfo(studentstu[],int*stuNum,intsubNum);/*修改学生信息*/
statusdescSortAndRanking(studentstu[],intstuNum,intsubNum);/*按总分降序排列并录入名次*/
statusprintStatistics(studentstu[],intstuNum,intsubNum);/*输出统计数据*/
intgetCommand(void);/*输入命令编号*/
statusprintMenuText(void);/*打印菜单文本*/
statusrunMeun(studentstu[],int*stuNum,intsubNum);/*运行菜单*/
statuspressAnykeyToContinue(void);/*按任意键继续*/
/**********************以上为函数声明**********************/
主界面
⑦ 学习C语言是为了什么服务呢
个人看法:
学习C语言,让人真正地了解计算机、掌握计算机;
学习C语言,让人知道什么是程序,如何编写程序;
学习C语言,让人更容易进一步学习汇编语言;
学习C语言,让人更好地掌握和设计数据结构与算法。
学习C语言。。。。。。
所以,C语言是计算机的根本之一,抓住了这个根本,有关计算机的东西就可以举一反三、触类旁通。
⑧ 怎么用c语言检测某服务是否启动
两种途径,一种是和服务约定探测请求接口,定时的发探测请求来通过是否获得响应判断是否服务存在;另外一种是通过shell去判断服务进程是否存在
⑨ 如何用c语言实现http服务器
//服务端简易代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<err.h>
#include<event.h>
#include<evhttp.h>
voidhttp_handle(structevhttp_request*req,void*arg);/*HTTPRequestHandle*/
intmain(){
structevhttp*httpd;
event_init();
httpd=evhttp_start("0.0.0.0",2345);
if(httpd==NULL){
fprintf(stderr,"Error:Unabletolistenon%s:%d ");
exit(1);
}
evhttp_set_timeout(httpd,2000);
evhttp_set_gencb(httpd,http_handle,NULL);
event_dispatch();
evhttp_free(httpd);
return0;
}
voidhttp_handle(structevhttp_request*req,void*arg){
structevbuffer*buf;
buf=evbuffer_new();
/*Responsetheclient*/
evhttp_send_reply(req,HTTP_OK,"OK",buf);
//evbuffer_add_printf(buf,"%s","HTTPSQS_AUTH_FAILED");
/*Releasethememory*/
evbuffer_free(buf);
fprintf(stderr,"Send ");
}
编译:编译时把libevent的类库中的.so文件和.h文件连接进来。