/* fpont 1/00 */
/* pont.net */
/* tcpServer.c */

#include
#include
#include
#include
#include
#include
#include /* close */


#define SUCCESS 0
#define ERROR 1

#define END_LINE 0x0
#define SERVER_PORT 1500
#define MAX_MSG 100

/* function readline */
int read_line();

int string_length(char string[]) {
int i;

i = 0;
while (string[i] != '\0') i++;

return i;
}

void string_concat(char st1[], char st2[], char result[]) {
int i, j;

i = 0;
while ( (result[i] = st1[i]) != '\0' ) i++;
j = i;
while ( (result[i] = st2[i - j]) != '\0' ) i++;
}

void substring(char string[], int start, int length, char result[]) {
int i;

if ( string_length(string) <= start) {
result[0] = '\0';
return;
}

i = 0;
while ( i < length && (result[i] = string[start + i]) != '\0' ) i++;

result[i] = '\0';
}




main( int argc ,char *argv[])
{
int sockfd;
int size;
struct sockaddr_in dest;
char buffer[] = "post ok!";
char buf[255];
char cmd[255];
char st1[1];
char st2[20];
int st1_i;
/* create socket , same as client */
sockfd = socket(PF_INET, SOCK_STREAM, 0);

/* initialize structure dest */
bzero(&dest, sizeof(dest));
dest.sin_family = PF_INET;
dest.sin_port = htons(8890);
/* this line is different from client */
dest.sin_addr.s_addr = INADDR_ANY;

/* Assign a port number to socket */
bind(sockfd, (struct sockaddr*)&dest, sizeof(dest));

/* make it listen to socket with max 20 connections */
listen(sockfd, 20);
/* infinity loop -- accepting connection from client forever */
while(1)
{
int clientfd;
struct sockaddr_in client_addr;
int addrlen = sizeof(client_addr);
printf("waiting connecting ...");
/* Wait and Accept connection */
clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);

size= recv(clientfd, buf , 30,0);

/* Send message */
substring(buf , 0 , 1 , st1);
printf(">%s\n",st1);
substring(buf , 1 , 20, st2);
printf(">%s\n",st2);
st1_i = atoi(st1);
if ( st1_i == 1 ) {
string_concat("fglrun /u/siltron/aim/4gi41/pda_aimt370S ", st2, cmd);
printf("1>>%s\n",cmd);
}
if ( st1_i == 2 ) {
string_concat("fglrun /u/siltron/aim/4gi41/pda_aimt306Y ", st2, cmd);
printf("2>>%s\n",cmd);
}
if ( st1_i == 3 ) {
string_concat("fglrun /u/siltron/aim/4gi41/pda_aimt306S ", st2, cmd);
printf("3>>%s\n",cmd);
}
if ( st1_i == 4 ) {
string_concat("fglrun /u/siltron/aim/4gi41/pda_aimt309S ", st2, cmd);
printf("4>>%s\n",cmd);
}
if ( st1_i == 5 ) {
string_concat("fglrun /u/siltron/aim/4gi41/pda_aimt324S ", st2, cmd);
printf("5>>%s\n",cmd);
}
if ( st1_i == 6 ) {
string_concat("fglrun /u/siltron/apm/4gi41/pda_apmt722Y ", st2, cmd);
printf("6>>%s\n",cmd);
}
system(cmd);

send(clientfd, buffer, sizeof(buffer), 0);

/* close(client) */
close(clientfd);

}

/* close(server) , but never get here because of the loop */
close(sockfd);
return 0;
}



要編釋這個程式,使用指令: gcc sample-server.c -o sample-server
要執行程式可以使用指令: ./sample-server
http://www.linuxhall.org/modules.php?name=News&file=article&sid=79
arrow
arrow
    全站熱搜

    clinno 發表在 痞客邦 留言(0) 人氣()