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文件連接進來。