这是我的推广链接
http://www.myqihang.com/bbs/?fromuser=swetter
经常在这里的Delphi板块逛的,不再奔跑老大发的视频都挺不错
大家可以去下来看看
May 8, 2009
May 2, 2009
ProC
这个ProC就是简单的将一段范例代码做了很小很小的修改
makefile文件也要相应的增加
/* -------------------------------------------------------
* myproc.pc
* Author: Youbin Wang
* Rem : 2009.04.28 create the file
* ------------------------------------------------------- */
#include
#include
#include
#include
#include
#include
#include
#include
#include "mystructs.h"
EXEC SQL BEGIN DECLARE SECTION;
#define UNAME_LEN 20
#define PWD_LEN 40
VARCHAR username[UNAME_LEN];
VARCHAR password[PWD_LEN];
/* host structure for output value */
struct
{
VARCHAR emp_name[UNAME_LEN];
float salary;
float commission;
}emprec;
/* indicator structure to correspond to host output struct */
struct
{
short emp_name_ind;
short sal_ind;
short comm_ind;
}emprec_ind;
int emp_number;
int total_queried;
int i_num;
EXEC SQL END DECLARE SECTION;
/* Sql communications area.
you car user #include or EXEC SQL INCLUDE.*/
#include
int sql_error(msg)
char *msg;
{
char err_msg[128];
size_t buf_len, msg_len;
EXEC SQL WHENEVER SQLERROR CONTINUE;
printf("\n%s\n", msg);
buf_len = sizeof(err_msg);
sqlglm(err_msg, &buf_len, &msg_len);
printf("%.*s\n", msg_len, err_msg);
EXEC SQL ROLLBACK RELEASE;
exit(EXIT_FAILURE);
}
int srv_to_proc(int input,int s_socket)
{
char temp_char[32];
/* connect to ORACLE */
strncpy((char*)username.arr,"SCOTT",UNAME_LEN);
username.len =
(unsigned short)strlen((char*)username.arr);
strncpy((char*)password.arr,"TIGER",PWD_LEN);
password.len =
(unsigned short)strlen((char*)password.arr);
EXEC SQL CONNECT :username IDENTIFIED BY :password;
printf("\nConnected to ORACLE as user:%s \n",username.arr);
total_queried = 0;
EXEC SQL WHENEVER NOT FOUND GOTO notfound;
printf("\n\nemployee Salary Commission\n");
printf("-------- ------- ----------\n");
i_num=input;
EXEC SQL SELECT ename, sal, comm
INTO :emprec INDICATOR :emprec_ind
FROM EMP WHERE rownum = :i_num ;
emprec.emp_name.arr[emprec.emp_name.len] = '\0';
printf("%s %7.2f ",
emprec.emp_name.arr, emprec.salary);
if (-1 == emprec_ind.comm_ind)
printf("NULL\n");
else
printf("%7.2f\n", emprec.commission);
send(s_socket,(char *)&emprec, sizeof(emprec), 0);
notfound:
printf("\nNot data left - try again.\n");
printf("\n\nTotal rows returned was %d.\n", total_queried);
printf("\n bye bye. \n\n\n");
//Disconnect from ORACLE
EXEC SQL ROLLBACK WORK RELEASE;
exit(EXIT_SUCCESS);
}
makefile文件也要相应的增加
/* -------------------------------------------------------
* myproc.pc
* Author: Youbin Wang
* Rem : 2009.04.28 create the file
* ------------------------------------------------------- */
#include
#include
#include
#include
#include
#include
#include
#include
#include "mystructs.h"
EXEC SQL BEGIN DECLARE SECTION;
#define UNAME_LEN 20
#define PWD_LEN 40
VARCHAR username[UNAME_LEN];
VARCHAR password[PWD_LEN];
/* host structure for output value */
struct
{
VARCHAR emp_name[UNAME_LEN];
float salary;
float commission;
}emprec;
/* indicator structure to correspond to host output struct */
struct
{
short emp_name_ind;
short sal_ind;
short comm_ind;
}emprec_ind;
int emp_number;
int total_queried;
int i_num;
EXEC SQL END DECLARE SECTION;
/* Sql communications area.
you car user #include or EXEC SQL INCLUDE.*/
#include
int sql_error(msg)
char *msg;
{
char err_msg[128];
size_t buf_len, msg_len;
EXEC SQL WHENEVER SQLERROR CONTINUE;
printf("\n%s\n", msg);
buf_len = sizeof(err_msg);
sqlglm(err_msg, &buf_len, &msg_len);
printf("%.*s\n", msg_len, err_msg);
EXEC SQL ROLLBACK RELEASE;
exit(EXIT_FAILURE);
}
int srv_to_proc(int input,int s_socket)
{
char temp_char[32];
/* connect to ORACLE */
strncpy((char*)username.arr,"SCOTT",UNAME_LEN);
username.len =
(unsigned short)strlen((char*)username.arr);
strncpy((char*)password.arr,"TIGER",PWD_LEN);
password.len =
(unsigned short)strlen((char*)password.arr);
EXEC SQL CONNECT :username IDENTIFIED BY :password;
printf("\nConnected to ORACLE as user:%s \n",username.arr);
total_queried = 0;
EXEC SQL WHENEVER NOT FOUND GOTO notfound;
printf("\n\nemployee Salary Commission\n");
printf("-------- ------- ----------\n");
i_num=input;
EXEC SQL SELECT ename, sal, comm
INTO :emprec INDICATOR :emprec_ind
FROM EMP WHERE rownum = :i_num ;
emprec.emp_name.arr[emprec.emp_name.len] = '\0';
printf("%s %7.2f ",
emprec.emp_name.arr, emprec.salary);
if (-1 == emprec_ind.comm_ind)
printf("NULL\n");
else
printf("%7.2f\n", emprec.commission);
send(s_socket,(char *)&emprec, sizeof(emprec), 0);
notfound:
printf("\nNot data left - try again.\n");
printf("\n\nTotal rows returned was %d.\n", total_queried);
printf("\n bye bye. \n\n\n");
//Disconnect from ORACLE
EXEC SQL ROLLBACK WORK RELEASE;
exit(EXIT_SUCCESS);
}
Apr 25, 2009
改写为C/S模式
上次贴的C代码有不少不规范的地方
随着学习的深入我把代码改写了下,写成C/S模式,但是运行时客户端只能连上一次服务器然后就断开。正在DEBUG
----------------------------makefile-------------------------------------------
# This makefile will generate "filetool" with fileoperator.c and fileoperator.h
# Author: Youbin Wang
# Date: 2009.04.17
OBJECTS=menu.o puberr.o simplecli.o
INCLUDE=-I/home/user01/NO05/include
MYPATH=/home/user01/NO05/bin
SERVEROBJ=simplesrv.o
all:filetool server
filetool: $(OBJECTS)
gcc -o filetool $(OBJECTS)
mv filetool $(MYPATH)
menu.o:menu.c
gcc $(INCLUDE) -c menu.c
`
puberr.o:puberr.c
gcc $(INCLUDE) -c puberr.c
simplecli.o:simplecli.c
gcc $(INCLUDE) -c simplecli.c
server : $(SERVEROBJ) puberr.o
gcc -o server $(SERVEROBJ) puberr.o
mv server $(MYPATH)
simplesrv.o:simplesrv.c
gcc $(INCLUDE) -c simplesrv.c
clean:
rm $(OBJECTS) $(SERVEROBJ)
------------------------------------menu.c-----------------------------------
/* -------------------------------------------------------
* $Source : menu.c 16-apr-2009.17:00:00 Youbin Wang $
* Function: Provide main menu and invoke client socket function
* Author : Youbin Wang
* Record :
2009.04.24 Change programme to c/s structure.
* 2009.04.22 Add errlog.
* 2009.04.16 Add the Version commentary.
* 2009.04.15 Create the main programme.
* ------------------------------------------------------- */
#include
#include
#include
#include
#include
/* -------------------------------------------------------
* global values
* ------------------------------------------------------- */
char * g_syslog="mytest";
char * g_print_str="testfile";
/* -------------------------------------------------------
* Show main menu if user haven't decided to exit the system
* or they just finish one operation
* Input parameter: none
* Output Parameter: return 0 while execute successfully
* ------------------------------------------------------- */
int show_menu()
{
printf(" --------------------------------------------\n");
printf(" Welcome TO This Programme \n");
printf(" This is the Main Menu \n");
printf(" --------------------------------------------\n");
printf("\n");
printf("Please Make your selection\n");
printf(" 1: Create a new file and write;\n");
printf(" 2: Open a file and write;\n");
printf(" 3: Open a file with read-only mode;\n");
printf(" 4: Exit;\n");
printf("\n");
return 0;
}
/* -------------------------------------------------------
* if the user choose to exit the system then invoke this function
* InputParameter:None
* OutputParameter: retrun 0 while execute successfully
* ------------------------------------------------------- */
int exit_system()
{
printf("\n");
printf("Exiting.........\n");
printf("\n");
printf(" Thank you for using this system.\n");
printf("\n");
printf(" If you have any problems or advices,contract the author at qisi1986@gmail.com\n");
printf("\n");
printf(" Wish you a good day. Bye Bye.\n");
return 0;
}
/* -------------------------------------------------------
* invoke if the user input the selection
* InputParamete r: the selection of user
* OutputParameter: return 0 while successfully
* Hint : if the user put the wrong key then it will provide hints
* ------------------------------------------------------- */
int selection(int Index)
{
int i_return=1;
printf("Your Selection is %d \n", Index);
printf("Now will invoke corresponding function,please wait.");
printf("\n");
sleep(1);
switch(Index)
{
case 1:
i_return = use_socket("127.0.0.1",1);
break;
case 2:
i_return = use_socket("127.0.0.1",2);
break;
case 3:
i_return = use_socket("127.0.0.1",3);
break;
case 4:
exit_system();
break;
default:
printf("Error!Please Enter a correct number!\n");
pub_err(2,__FILE__,__LINE__,g_syslog,"Enter an error number %s\n",g_print_str);
sleep(1);
}
if(0 == i_return)
printf("Operation successfully!!\n");
else
{
printf("Opeartion failed, please retry!\n");
i_return = 1;
return 1;
}
i_return = 1;
return 0;
}
int main()
{
int inputInt;
system("clear");
while(inputInt != 4)
{
system("clear");
show_menu();
printf("Enter your selection here: ");
scanf("%d", &inputInt);
selection(inputInt);
}
return 0;
}
-------------------------------puberr.c----------------------------------
#include
#include
#include
#include
#include
#include
#define FNAME_LEN 128
void pub_time(long *plHostDate, long *plHostTime)
{
struct tm *p;
time_t tmp;
time(&(tmp));
p = localtime(&tmp);
p->tm_year += 1899;
if (plHostDate != NULL)
*plHostDate =
(p->tm_year + 1) * 10000 + (p->tm_mon + 1) * 100 + p->tm_mday;
if (plHostTime != NULL)
*plHostTime = p->tm_hour * 10000 + p->tm_min * 100 + p->tm_sec;
return;
}
int _p_log(char *tmp_buf)
{
char logname[FNAME_LEN];
FILE *fp_log;
long l_date, l_time;
/*错误日志 */
pub_time(&l_date, &l_time);
sprintf(logname, "%s/log/pub_err%ld.log", getenv("HOME"),l_date);
// sprintf(logname, "../log/pub_err%ld.log", l_date);
if ((fp_log = fopen(logname, "a")) != NULL) {
fprintf(fp_log, "[%06ld/%06ld] %s\n", l_date % 1000000, l_time, tmp_buf);
fclose(fp_log);
}
else
{
fprintf(stderr, "[%06ld/%06ld] %s\n", l_date % 1000000, l_time, tmp_buf);
}
return 0;
}
/*
int pub_err(ret_code, file, line, recode, ...)
int ret_code;
char *file;
long line;
char *recode
*/
int pub_err(int ret_code, char *file, long line, char *recode, ...)
{
short i;
char *fmt, tmp_buf[2048], log[2048];
va_list args;
memset(tmp_buf, 0, 2048);
va_start(args,recode);
fmt = va_arg(args, char *);
vsprintf(tmp_buf, fmt, args);
va_end(args);
if (ret_code >= 0)
{
sprintf(log, "run message [%s][%ld][%s]", file, line,tmp_buf);
_p_log(log);
}
return ret_code;
}
------------------------------simplecli.c--------------------------------
/* ----------------------------------------------------------------
* simplecli.c
* Function:provide use_socket to set up a client socket
* and send the selection of user to server
* Record of Modification:
* 2009.04.24:Create function user_socket and test main
* ---------------------------------------------------------------- */
#include
#include
#include
#include
#include
#include
// define the defualt connect port id
#define SERVER_PORT 20130
// define the defualt client port as a random port
#define CLIENT_PORT ((20001+rand())%65536)
#define BUFFER_SIZE 255
#define REUQEST_MESSAGE "welcome to connect the server.\n"
int use_socket(char* ip, int i_select)
{
int servfd,clifd,length = 0;
int recvbyte;
struct sockaddr_in servaddr,cliaddr;
socklen_t socklen = sizeof(servaddr);
char buf[BUFFER_SIZE];
if ((clifd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("create socket error!\n");
exit(1);
}
srand(time(NULL));//initialize random generator
bzero(&cliaddr,sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(CLIENT_PORT);
cliaddr.sin_addr.s_addr = htons(INADDR_ANY);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
//inet_aton(argv[1],&servaddr.sin_addr);
//servaddr.sin_addr.s_addr=inet_addr(ip);
inet_aton(ip,&servaddr.sin_addr);
servaddr.sin_port = htons(SERVER_PORT);
//servaddr.sin_addr.s_addr = htons(INADDR_ANY);
if (bind(clifd,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0)
{
printf("bind to port %d failure!\n",CLIENT_PORT);
exit(1);
}
if (connect(clifd,(struct sockaddr*)&servaddr, socklen) < 0)
{
printf("can't connect to %s!\n",ip);
exit(1);
}
send(servfd,(char *)&i_select,sizeof(i_select),0);
length = recv(clifd,(char *)&recvbyte,sizeof(recvbyte),0);
if(length < 0)
{
printf("error comes when recieve data from server %s!", ip);
exit(1);
}
if(recvbyte == 0)
{
close(clifd);
return 0;
}
else
{
close(clifd);
exit(1);
}
}
/*-----------this is the test main function of use_socket-----------
int main()
{
int i=3;
if(use_socket("127.0.0.1", i))
printf("successful");
else
printf("failed");
return 0;
}-------------------------------------------------------------------*/
--------------------------simplesrv.c-------------------------------------
/* -------------------------------------------------------
* simplesrv.c
* Function:Start the server and listen a port
* When client send data, it recieve data
* deal with the data and send feedback info
* Record of Modification:
* 2009.04.23: Create it.
* -------------------------------------------------------*/
#include
#include
#include
#include
#include
#include
#include
#include "puberr.h"
#define SERVER_PORT 20130 // define the defualt connect port id
#define LENGTH_OF_LISTEN_QUEUE 10 //length of listen queue in server
#define BUFFER_SIZE 255
#define WELCOME_MESSAGE "welcome to connect the server. "
/* -------------------------------------------------------
* global values
* ------------------------------------------------------- */
char * g_syslog="mytest";
char * g_print_str="testfile";
/* -------------------------------------------------------
* invoke when the user choose to open a file with readonly mode(3)
* InputParameter:None
* OutputParameter: return 0 if execute successfully, 1 if error occured
* ------------------------------------------------------- */
int open_file_readonly()
{
char c_output;
FILE *fl_p_readonly;
fl_p_readonly = fopen("mytext.txt","rt");
printf("Reading mytext.txt ...\n");
if(NULL==fl_p_readonly)
{
pub_err(2, __FILE__, __LINE__, g_syslog, "Cannot open file mytext.txt in read-only mode %s\n", g_print_str);
getchar();
return 1;
}
printf("Open file successfully! \n");
sleep(1);
c_output=fgetc(fl_p_readonly);
while(c_output!=EOF)
{
printf("Reading the text...\n");
sleep(1);
putchar(c_output);
c_output=fgetc(fl_p_readonly);
}
fclose(fl_p_readonly);
pub_err(2, __FILE__, __LINE__, g_syslog,"Close the file %s\n", g_print_str);
return 0;
}
/* -------------------------------------------------------
* invoke when the user choose to create a new file(1)
* InputParameter: None
* OutputParameter: return 0 if execute successfully, 1 if error accured
* -------------------------------------------------------*/
int create_file()
{
printf("Creating a file ..\n");
FILE *fl_createfile;
fl_createfile = fopen("new.txt","wt+");
if(NULL==fl_createfile)
{
pub_err(2, __FILE__, __LINE__, g_syslog, "Cannot create a file %s\n",g_print_str);
getchar();
return 1;
}
char c_inputchar;
printf("Successfully create a file...\n");
sleep(1);
printf("Please input your text: \n");
while (c_inputchar!='\n')
{
printf("I'm reading your input\n");
fputc(c_inputchar,fl_createfile);
c_inputchar=getchar();
}
/* rewind the position of file pointer to the top of file */
rewind(fl_createfile);
printf("Your inputment is: ");
c_inputchar=fgetc(fl_createfile);
while(c_inputchar!=EOF)
{
printf("I'm writing your input\n");
putchar(c_inputchar);
c_inputchar=fgetc(fl_createfile);
}
printf("\n");
fclose(fl_createfile);
printf("Done!!\n");
sleep(1);
return 0;
}
/* -------------------------------------------------------
* if the user choose to open a exist file(2) and write then invoke this function
* InputParameter:None
* OutputParameter: return 0 if execute successfully, 1 if an error occured
* ------------------------------------------------------- */
int open_file()
{
printf("Opening a file...\n");
FILE *fl_openfile;
fl_openfile = fopen("mytext.txt","wt+");
if(NULL==fl_openfile)
{
pub_err(2,__FILE__,__LINE__,g_syslog,"Cannot open a file %s\n",g_print_str);
getchar();
return 1;
}
char c_inputchar;
printf("Successfully open a file");
printf("Please input your text: \n");
while (c_inputchar!='\n')
{
fputc(c_inputchar,fl_openfile);
c_inputchar=getchar();
}
/* rewind the position of file pointer to the top of file */
rewind(fl_openfile);
printf("Your inputment is: ");
c_inputchar=fgetc(fl_openfile);
while(c_inputchar!=EOF)
{
putchar(c_inputchar);
c_inputchar=fgetc(fl_openfile);
}
printf("\n");
fclose(fl_openfile);
printf("Done!!\n");
sleep(1);
return 0;
}
/* -------------------------------------------------------
* invoke if the user input the selection
* InputParamete r: the selection of user
* OutputParameter: return 0 while successfully
* Hint : if the user put the wrong key then it will provide hints
* ------------------------------------------------------- */
int selection(int i_input)
{
switch(i_input)
{
case 1:
create_file();
break;
case 2:
open_file();
break;
case 3:
open_file_readonly();
break;
default:
pub_err(2,__FILE__,__LINE__,g_syslog,"Enter an error number %s\n",g_print_str);
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
int servfd,clifd,recvbyte;
int length=0;
int sel_feedback=1;
struct sockaddr_in servaddr,cliaddr;
if ((servfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("create socket error!\n");
exit(1);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERVER_PORT);
servaddr.sin_addr.s_addr = htons(INADDR_ANY);
if (bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
{
printf("bind to port %d failure!\n",SERVER_PORT);
exit(1);
}
if (listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0)
{
printf("call listen failure!\n");
exit(1);
}
while (1)
{//server loop will nerver exit unless any body kill the process
char buf[BUFFER_SIZE];
long timestamp;
socklen_t length = sizeof(cliaddr);
clifd = accept(servfd,(struct sockaddr*)&cliaddr,&length);
if (clifd < 0)
{
printf("error comes when call accept!\n");
break;
}
strcpy(buf,WELCOME_MESSAGE);
//inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE);
printf("from client,IP:%s,Port:%d\n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
timestamp = time(NULL);
strcat(buf,"timestamp in server:");
strcat(buf,ctime(×tamp));
//send(clifd,buf,BUFFER_SIZE,0);
length=recv(servfd,(char *)&recvbyte,sizeof(recvbyte),0);
if(length<0)
{
printf("error comes when recieve data from client");
close(clifd);
exit(1);
}
sel_feedback = selection(recvbyte);
send(clifd,(char *)sel_feedback,sizeof(sel_feedback),0);
close(clifd);
sel_feedback = 1;
}//exit
close(servfd);
return 0;
}
随着学习的深入我把代码改写了下,写成C/S模式,但是运行时客户端只能连上一次服务器然后就断开。正在DEBUG
----------------------------makefile-------------------------------------------
# This makefile will generate "filetool" with fileoperator.c and fileoperator.h
# Author: Youbin Wang
# Date: 2009.04.17
OBJECTS=menu.o puberr.o simplecli.o
INCLUDE=-I/home/user01/NO05/include
MYPATH=/home/user01/NO05/bin
SERVEROBJ=simplesrv.o
all:filetool server
filetool: $(OBJECTS)
gcc -o filetool $(OBJECTS)
mv filetool $(MYPATH)
menu.o:menu.c
gcc $(INCLUDE) -c menu.c
`
puberr.o:puberr.c
gcc $(INCLUDE) -c puberr.c
simplecli.o:simplecli.c
gcc $(INCLUDE) -c simplecli.c
server : $(SERVEROBJ) puberr.o
gcc -o server $(SERVEROBJ) puberr.o
mv server $(MYPATH)
simplesrv.o:simplesrv.c
gcc $(INCLUDE) -c simplesrv.c
clean:
rm $(OBJECTS) $(SERVEROBJ)
------------------------------------menu.c-----------------------------------
/* -------------------------------------------------------
* $Source : menu.c 16-apr-2009.17:00:00 Youbin Wang $
* Function: Provide main menu and invoke client socket function
* Author : Youbin Wang
* Record :
2009.04.24 Change programme to c/s structure.
* 2009.04.22 Add errlog.
* 2009.04.16 Add the Version commentary.
* 2009.04.15 Create the main programme.
* ------------------------------------------------------- */
#include
#include
#include
#include
#include
/* -------------------------------------------------------
* global values
* ------------------------------------------------------- */
char * g_syslog="mytest";
char * g_print_str="testfile";
/* -------------------------------------------------------
* Show main menu if user haven't decided to exit the system
* or they just finish one operation
* Input parameter: none
* Output Parameter: return 0 while execute successfully
* ------------------------------------------------------- */
int show_menu()
{
printf(" --------------------------------------------\n");
printf(" Welcome TO This Programme \n");
printf(" This is the Main Menu \n");
printf(" --------------------------------------------\n");
printf("\n");
printf("Please Make your selection\n");
printf(" 1: Create a new file and write;\n");
printf(" 2: Open a file and write;\n");
printf(" 3: Open a file with read-only mode;\n");
printf(" 4: Exit;\n");
printf("\n");
return 0;
}
/* -------------------------------------------------------
* if the user choose to exit the system then invoke this function
* InputParameter:None
* OutputParameter: retrun 0 while execute successfully
* ------------------------------------------------------- */
int exit_system()
{
printf("\n");
printf("Exiting.........\n");
printf("\n");
printf(" Thank you for using this system.\n");
printf("\n");
printf(" If you have any problems or advices,contract the author at qisi1986@gmail.com\n");
printf("\n");
printf(" Wish you a good day. Bye Bye.\n");
return 0;
}
/* -------------------------------------------------------
* invoke if the user input the selection
* InputParamete r: the selection of user
* OutputParameter: return 0 while successfully
* Hint : if the user put the wrong key then it will provide hints
* ------------------------------------------------------- */
int selection(int Index)
{
int i_return=1;
printf("Your Selection is %d \n", Index);
printf("Now will invoke corresponding function,please wait.");
printf("\n");
sleep(1);
switch(Index)
{
case 1:
i_return = use_socket("127.0.0.1",1);
break;
case 2:
i_return = use_socket("127.0.0.1",2);
break;
case 3:
i_return = use_socket("127.0.0.1",3);
break;
case 4:
exit_system();
break;
default:
printf("Error!Please Enter a correct number!\n");
pub_err(2,__FILE__,__LINE__,g_syslog,"Enter an error number %s\n",g_print_str);
sleep(1);
}
if(0 == i_return)
printf("Operation successfully!!\n");
else
{
printf("Opeartion failed, please retry!\n");
i_return = 1;
return 1;
}
i_return = 1;
return 0;
}
int main()
{
int inputInt;
system("clear");
while(inputInt != 4)
{
system("clear");
show_menu();
printf("Enter your selection here: ");
scanf("%d", &inputInt);
selection(inputInt);
}
return 0;
}
-------------------------------puberr.c----------------------------------
#include
#include
#include
#include
#include
#include
#define FNAME_LEN 128
void pub_time(long *plHostDate, long *plHostTime)
{
struct tm *p;
time_t tmp;
time(&(tmp));
p = localtime(&tmp);
p->tm_year += 1899;
if (plHostDate != NULL)
*plHostDate =
(p->tm_year + 1) * 10000 + (p->tm_mon + 1) * 100 + p->tm_mday;
if (plHostTime != NULL)
*plHostTime = p->tm_hour * 10000 + p->tm_min * 100 + p->tm_sec;
return;
}
int _p_log(char *tmp_buf)
{
char logname[FNAME_LEN];
FILE *fp_log;
long l_date, l_time;
/*错误日志 */
pub_time(&l_date, &l_time);
sprintf(logname, "%s/log/pub_err%ld.log", getenv("HOME"),l_date);
// sprintf(logname, "../log/pub_err%ld.log", l_date);
if ((fp_log = fopen(logname, "a")) != NULL) {
fprintf(fp_log, "[%06ld/%06ld] %s\n", l_date % 1000000, l_time, tmp_buf);
fclose(fp_log);
}
else
{
fprintf(stderr, "[%06ld/%06ld] %s\n", l_date % 1000000, l_time, tmp_buf);
}
return 0;
}
/*
int pub_err(ret_code, file, line, recode, ...)
int ret_code;
char *file;
long line;
char *recode
*/
int pub_err(int ret_code, char *file, long line, char *recode, ...)
{
short i;
char *fmt, tmp_buf[2048], log[2048];
va_list args;
memset(tmp_buf, 0, 2048);
va_start(args,recode);
fmt = va_arg(args, char *);
vsprintf(tmp_buf, fmt, args);
va_end(args);
if (ret_code >= 0)
{
sprintf(log, "run message [%s][%ld][%s]", file, line,tmp_buf);
_p_log(log);
}
return ret_code;
}
------------------------------simplecli.c--------------------------------
/* ----------------------------------------------------------------
* simplecli.c
* Function:provide use_socket to set up a client socket
* and send the selection of user to server
* Record of Modification:
* 2009.04.24:Create function user_socket and test main
* ---------------------------------------------------------------- */
#include
#include
#include
#include
#include
#include
// define the defualt connect port id
#define SERVER_PORT 20130
// define the defualt client port as a random port
#define CLIENT_PORT ((20001+rand())%65536)
#define BUFFER_SIZE 255
#define REUQEST_MESSAGE "welcome to connect the server.\n"
int use_socket(char* ip, int i_select)
{
int servfd,clifd,length = 0;
int recvbyte;
struct sockaddr_in servaddr,cliaddr;
socklen_t socklen = sizeof(servaddr);
char buf[BUFFER_SIZE];
if ((clifd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("create socket error!\n");
exit(1);
}
srand(time(NULL));//initialize random generator
bzero(&cliaddr,sizeof(cliaddr));
cliaddr.sin_family = AF_INET;
cliaddr.sin_port = htons(CLIENT_PORT);
cliaddr.sin_addr.s_addr = htons(INADDR_ANY);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
//inet_aton(argv[1],&servaddr.sin_addr);
//servaddr.sin_addr.s_addr=inet_addr(ip);
inet_aton(ip,&servaddr.sin_addr);
servaddr.sin_port = htons(SERVER_PORT);
//servaddr.sin_addr.s_addr = htons(INADDR_ANY);
if (bind(clifd,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0)
{
printf("bind to port %d failure!\n",CLIENT_PORT);
exit(1);
}
if (connect(clifd,(struct sockaddr*)&servaddr, socklen) < 0)
{
printf("can't connect to %s!\n",ip);
exit(1);
}
send(servfd,(char *)&i_select,sizeof(i_select),0);
length = recv(clifd,(char *)&recvbyte,sizeof(recvbyte),0);
if(length < 0)
{
printf("error comes when recieve data from server %s!", ip);
exit(1);
}
if(recvbyte == 0)
{
close(clifd);
return 0;
}
else
{
close(clifd);
exit(1);
}
}
/*-----------this is the test main function of use_socket-----------
int main()
{
int i=3;
if(use_socket("127.0.0.1", i))
printf("successful");
else
printf("failed");
return 0;
}-------------------------------------------------------------------*/
--------------------------simplesrv.c-------------------------------------
/* -------------------------------------------------------
* simplesrv.c
* Function:Start the server and listen a port
* When client send data, it recieve data
* deal with the data and send feedback info
* Record of Modification:
* 2009.04.23: Create it.
* -------------------------------------------------------*/
#include
#include
#include
#include
#include
#include
#include
#include "puberr.h"
#define SERVER_PORT 20130 // define the defualt connect port id
#define LENGTH_OF_LISTEN_QUEUE 10 //length of listen queue in server
#define BUFFER_SIZE 255
#define WELCOME_MESSAGE "welcome to connect the server. "
/* -------------------------------------------------------
* global values
* ------------------------------------------------------- */
char * g_syslog="mytest";
char * g_print_str="testfile";
/* -------------------------------------------------------
* invoke when the user choose to open a file with readonly mode(3)
* InputParameter:None
* OutputParameter: return 0 if execute successfully, 1 if error occured
* ------------------------------------------------------- */
int open_file_readonly()
{
char c_output;
FILE *fl_p_readonly;
fl_p_readonly = fopen("mytext.txt","rt");
printf("Reading mytext.txt ...\n");
if(NULL==fl_p_readonly)
{
pub_err(2, __FILE__, __LINE__, g_syslog, "Cannot open file mytext.txt in read-only mode %s\n", g_print_str);
getchar();
return 1;
}
printf("Open file successfully! \n");
sleep(1);
c_output=fgetc(fl_p_readonly);
while(c_output!=EOF)
{
printf("Reading the text...\n");
sleep(1);
putchar(c_output);
c_output=fgetc(fl_p_readonly);
}
fclose(fl_p_readonly);
pub_err(2, __FILE__, __LINE__, g_syslog,"Close the file %s\n", g_print_str);
return 0;
}
/* -------------------------------------------------------
* invoke when the user choose to create a new file(1)
* InputParameter: None
* OutputParameter: return 0 if execute successfully, 1 if error accured
* -------------------------------------------------------*/
int create_file()
{
printf("Creating a file ..\n");
FILE *fl_createfile;
fl_createfile = fopen("new.txt","wt+");
if(NULL==fl_createfile)
{
pub_err(2, __FILE__, __LINE__, g_syslog, "Cannot create a file %s\n",g_print_str);
getchar();
return 1;
}
char c_inputchar;
printf("Successfully create a file...\n");
sleep(1);
printf("Please input your text: \n");
while (c_inputchar!='\n')
{
printf("I'm reading your input\n");
fputc(c_inputchar,fl_createfile);
c_inputchar=getchar();
}
/* rewind the position of file pointer to the top of file */
rewind(fl_createfile);
printf("Your inputment is: ");
c_inputchar=fgetc(fl_createfile);
while(c_inputchar!=EOF)
{
printf("I'm writing your input\n");
putchar(c_inputchar);
c_inputchar=fgetc(fl_createfile);
}
printf("\n");
fclose(fl_createfile);
printf("Done!!\n");
sleep(1);
return 0;
}
/* -------------------------------------------------------
* if the user choose to open a exist file(2) and write then invoke this function
* InputParameter:None
* OutputParameter: return 0 if execute successfully, 1 if an error occured
* ------------------------------------------------------- */
int open_file()
{
printf("Opening a file...\n");
FILE *fl_openfile;
fl_openfile = fopen("mytext.txt","wt+");
if(NULL==fl_openfile)
{
pub_err(2,__FILE__,__LINE__,g_syslog,"Cannot open a file %s\n",g_print_str);
getchar();
return 1;
}
char c_inputchar;
printf("Successfully open a file");
printf("Please input your text: \n");
while (c_inputchar!='\n')
{
fputc(c_inputchar,fl_openfile);
c_inputchar=getchar();
}
/* rewind the position of file pointer to the top of file */
rewind(fl_openfile);
printf("Your inputment is: ");
c_inputchar=fgetc(fl_openfile);
while(c_inputchar!=EOF)
{
putchar(c_inputchar);
c_inputchar=fgetc(fl_openfile);
}
printf("\n");
fclose(fl_openfile);
printf("Done!!\n");
sleep(1);
return 0;
}
/* -------------------------------------------------------
* invoke if the user input the selection
* InputParamete r: the selection of user
* OutputParameter: return 0 while successfully
* Hint : if the user put the wrong key then it will provide hints
* ------------------------------------------------------- */
int selection(int i_input)
{
switch(i_input)
{
case 1:
create_file();
break;
case 2:
open_file();
break;
case 3:
open_file_readonly();
break;
default:
pub_err(2,__FILE__,__LINE__,g_syslog,"Enter an error number %s\n",g_print_str);
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
int servfd,clifd,recvbyte;
int length=0;
int sel_feedback=1;
struct sockaddr_in servaddr,cliaddr;
if ((servfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("create socket error!\n");
exit(1);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERVER_PORT);
servaddr.sin_addr.s_addr = htons(INADDR_ANY);
if (bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
{
printf("bind to port %d failure!\n",SERVER_PORT);
exit(1);
}
if (listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0)
{
printf("call listen failure!\n");
exit(1);
}
while (1)
{//server loop will nerver exit unless any body kill the process
char buf[BUFFER_SIZE];
long timestamp;
socklen_t length = sizeof(cliaddr);
clifd = accept(servfd,(struct sockaddr*)&cliaddr,&length);
if (clifd < 0)
{
printf("error comes when call accept!\n");
break;
}
strcpy(buf,WELCOME_MESSAGE);
//inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE);
printf("from client,IP:%s,Port:%d\n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
timestamp = time(NULL);
strcat(buf,"timestamp in server:");
strcat(buf,ctime(×tamp));
//send(clifd,buf,BUFFER_SIZE,0);
length=recv(servfd,(char *)&recvbyte,sizeof(recvbyte),0);
if(length<0)
{
printf("error comes when recieve data from client");
close(clifd);
exit(1);
}
sel_feedback = selection(recvbyte);
send(clifd,(char *)sel_feedback,sizeof(sel_feedback),0);
close(clifd);
sel_feedback = 1;
}//exit
close(servfd);
return 0;
}
Apr 22, 2009
vi常用命令
进入vi的命令
vi filename :打开或新建文件,并将光标置于第一行首
vi +n filename :打开文件,并将光标置于第n行首
vi + filename :打开文件,并将光标置于最后一行首
vi +/pattern filename:打开文件,并将光标置于第一个与pattern匹配的串处
vi -r filename :在上次正用vi编辑时发生系统崩溃,恢复filename
vi filename....filename :打开多个文件,依次进行编辑
移动光标类命令[/b]
h :光标左移一个字符
l :光标右移一个字符
space:光标右移一个字符
Backspace:光标左移一个字符
k或Ctrl+p:光标上移一行
j或Ctrl+n :光标下移一行
Enter :光标下移一行
w或W :光标右移一个字至字首
b或B :光标左移一个字至字首
e或E :光标右移一个字至字尾
) :光标移至句尾
( :光标移至句首
}:光标移至段落开头
{:光标移至段落结尾
nG:光标移至第n行首
n+:光标下移n行
n-:光标上移n行
n$:光标移至第n行尾
H :光标移至屏幕顶行
M :光标移至屏幕中间行
L :光标移至屏幕最后行
0:(注意是数字零)光标移至当前行首
$:光标移至当前行尾
屏幕翻滚类命令
Ctrl+u:向文件首翻半屏
Ctrl+d:向文件尾翻半屏
Ctrl+f:向文件尾翻一屏
Ctrl+b;向文件首翻一屏
nz:将第n行滚至屏幕顶部,不指定n时将当前行滚至屏幕顶部。
插入文本类命令
i :在光标前
I :在当前行首
a:光标后
A:在当前行尾
o:在当前行之下新开一行
O:在当前行之上新开一行
r:替换当前字符
R:替换当前字符及其后的字符,直至按ESC键
s:从当前光标位置处开始,以输入的文本替代指定数目的字符
S:删除指定数目的行,并以所输入文本代替之
ncw或nCW:修改指定数目的字
nCC:修改指定数目的行
删除命令
ndw或ndW:删除光标处开始及其后的n-1个字
do:删至行首
d$:删至行尾
ndd:删除当前行及其后n-1行
x或X:删除一个字符,x删除光标后的,而X删除光标前的
Ctrl+u:删除输入方式下所输入的文本
搜索及替换命令
/pattern:从光标开始处向文件尾搜索pattern
?pattern:从光标开始处向文件首搜索pattern
n:在同一方向重复上一次搜索命令
N:在反方向上重复上一次搜索命令
:s/p1/p2/g:将当前行中所有p1均用p2替代
:n1,n2s/p1/p2/g:将第n1至n2行中所有p1均用p2替代
:g/p1/s//p2/g:将文件中所有p1均用p2替换
选项设置
all:列出所有选项设置情况
term:设置终端类型
ignorance:在搜索中忽略大小写
list:显示制表位(Ctrl+I)和行尾标志($)
number:显示行号
report:显示由面向行的命令修改过的数目
terse:显示简短的警告信息
warn:在转到别的文件时若没保存当前文件则显示NO write信息
nomagic:允许在搜索模式中,使用前面不带“\”的特殊字符
nowrapscan:禁止vi在搜索到达文件两端时,又从另一端开始
mesg:允许vi显示其他用户用write写到自己终端上的信息
最后行方式命令
:n1,n2 co n3:将n1行到n2行之间的内容拷贝到第n3行下
:n1,n2 m n3:将n1行到n2行之间的内容移至到第n3行下
:n1,n2 d :将n1行到n2行之间的内容删除
:w :保存当前文件
:e filename:打开文件filename进行编辑
:x:保存当前文件并退出
:q:退出vi
:q!:不保存文件并退出vi
:!command:执行shell命令command
:n1,n2 w!command:将文件中n1行至n2行的内容作为command的输入并执行之,若不指定n1,n2,则表示将整个文件内容作为command的输入
:r!command:将命令command的输出结果放到当前行
寄存器操作
"?nyy:将当前行及其下n行的内容保存到寄存器?中,其中?为一个字母,n为一个数字
"?nyw:将当前行及其下n个字保存到寄存器?中,其中?为一个字母,n为一个数字
"?nyl:将当前行及其下n个字符保存到寄存器?中,其中?为一个字母,n为一个数字
"?p:取出寄存器?中的内容并将其放到光标位置处。这里?可以是一个字母,也可以是一个数字
ndd:将当前行及其下共n行文本删除,并将所删内容放到1号删除寄存器中。
vi filename :打开或新建文件,并将光标置于第一行首
vi +n filename :打开文件,并将光标置于第n行首
vi + filename :打开文件,并将光标置于最后一行首
vi +/pattern filename:打开文件,并将光标置于第一个与pattern匹配的串处
vi -r filename :在上次正用vi编辑时发生系统崩溃,恢复filename
vi filename....filename :打开多个文件,依次进行编辑
移动光标类命令[/b]
h :光标左移一个字符
l :光标右移一个字符
space:光标右移一个字符
Backspace:光标左移一个字符
k或Ctrl+p:光标上移一行
j或Ctrl+n :光标下移一行
Enter :光标下移一行
w或W :光标右移一个字至字首
b或B :光标左移一个字至字首
e或E :光标右移一个字至字尾
) :光标移至句尾
( :光标移至句首
}:光标移至段落开头
{:光标移至段落结尾
nG:光标移至第n行首
n+:光标下移n行
n-:光标上移n行
n$:光标移至第n行尾
H :光标移至屏幕顶行
M :光标移至屏幕中间行
L :光标移至屏幕最后行
0:(注意是数字零)光标移至当前行首
$:光标移至当前行尾
屏幕翻滚类命令
Ctrl+u:向文件首翻半屏
Ctrl+d:向文件尾翻半屏
Ctrl+f:向文件尾翻一屏
Ctrl+b;向文件首翻一屏
nz:将第n行滚至屏幕顶部,不指定n时将当前行滚至屏幕顶部。
插入文本类命令
i :在光标前
I :在当前行首
a:光标后
A:在当前行尾
o:在当前行之下新开一行
O:在当前行之上新开一行
r:替换当前字符
R:替换当前字符及其后的字符,直至按ESC键
s:从当前光标位置处开始,以输入的文本替代指定数目的字符
S:删除指定数目的行,并以所输入文本代替之
ncw或nCW:修改指定数目的字
nCC:修改指定数目的行
删除命令
ndw或ndW:删除光标处开始及其后的n-1个字
do:删至行首
d$:删至行尾
ndd:删除当前行及其后n-1行
x或X:删除一个字符,x删除光标后的,而X删除光标前的
Ctrl+u:删除输入方式下所输入的文本
搜索及替换命令
/pattern:从光标开始处向文件尾搜索pattern
?pattern:从光标开始处向文件首搜索pattern
n:在同一方向重复上一次搜索命令
N:在反方向上重复上一次搜索命令
:s/p1/p2/g:将当前行中所有p1均用p2替代
:n1,n2s/p1/p2/g:将第n1至n2行中所有p1均用p2替代
:g/p1/s//p2/g:将文件中所有p1均用p2替换
选项设置
all:列出所有选项设置情况
term:设置终端类型
ignorance:在搜索中忽略大小写
list:显示制表位(Ctrl+I)和行尾标志($)
number:显示行号
report:显示由面向行的命令修改过的数目
terse:显示简短的警告信息
warn:在转到别的文件时若没保存当前文件则显示NO write信息
nomagic:允许在搜索模式中,使用前面不带“\”的特殊字符
nowrapscan:禁止vi在搜索到达文件两端时,又从另一端开始
mesg:允许vi显示其他用户用write写到自己终端上的信息
最后行方式命令
:n1,n2 co n3:将n1行到n2行之间的内容拷贝到第n3行下
:n1,n2 m n3:将n1行到n2行之间的内容移至到第n3行下
:n1,n2 d :将n1行到n2行之间的内容删除
:w :保存当前文件
:e filename:打开文件filename进行编辑
:x:保存当前文件并退出
:q:退出vi
:q!:不保存文件并退出vi
:!command:执行shell命令command
:n1,n2 w!command:将文件中n1行至n2行的内容作为command的输入并执行之,若不指定n1,n2,则表示将整个文件内容作为command的输入
:r!command:将命令command的输出结果放到当前行
寄存器操作
"?nyy:将当前行及其下n行的内容保存到寄存器?中,其中?为一个字母,n为一个数字
"?nyw:将当前行及其下n个字保存到寄存器?中,其中?为一个字母,n为一个数字
"?nyl:将当前行及其下n个字符保存到寄存器?中,其中?为一个字母,n为一个数字
"?p:取出寄存器?中的内容并将其放到光标位置处。这里?可以是一个字母,也可以是一个数字
ndd:将当前行及其下共n行文本删除,并将所删内容放到1号删除寄存器中。
Apr 21, 2009
Apr 20, 2009
Oracle Test05
其它数据库对象
1. 创建序列empno_seq,开始值为8000,每次增长2,最大值为9000,其他参数自选
2. 使用数据字典视图user_sequences查询序列的信息
3. 使用empno_seq向emp表中插入数据,要求empno的值为empno_seq的递增值,其他字段的值任意.
4. 为emp表中的员工姓名(ename)定义索引
5. 使用数据字典user_indexs查看索引的信息
6. 使用sys/sys身份登录,为scott.emp建立同义词emp_synm
1. 创建序列empno_seq,开始值为8000,每次增长2,最大值为9000,其他参数自选
2. 使用数据字典视图user_sequences查询序列的信息
3. 使用empno_seq向emp表中插入数据,要求empno的值为empno_seq的递增值,其他字段的值任意.
4. 为emp表中的员工姓名(ename)定义索引
5. 使用数据字典user_indexs查看索引的信息
6. 使用sys/sys身份登录,为scott.emp建立同义词emp_synm
Oracle Test04
视 图
1. 使用表emp创建视图emp_view,其中包括姓名(ename),员工号(empno,工资(sal),部门号(deptno).
2. 显示视图的结构
3. 查询数据字典视图user_views,检查视图的定义
4. 查询emp_view视图中的全部数据
5. 从emp_view中查询工资最高的三位员工信息
6. 修改视图emp_view,将视图中的数据限定在部门号是30的范围内
1. 使用表emp创建视图emp_view,其中包括姓名(ename),员工号(empno,工资(sal),部门号(deptno).
2. 显示视图的结构
3. 查询数据字典视图user_views,检查视图的定义
4. 查询emp_view视图中的全部数据
5. 从emp_view中查询工资最高的三位员工信息
6. 修改视图emp_view,将视图中的数据限定在部门号是30的范围内
Subscribe to:
Posts (Atom)