Introduction to network functions in C
http://shoe.bocks.com/net/
http://shoe.bocks.com/net/source/examples/chat.c
Tuesday, January 22, 2008
Monday, January 21, 2008
kbhit() for Linux
/*
Well I would just use ncurses, but you could possibly use below? Chances are you problly don't need either unless you're making a crappy console game, or a password program.
*/
#include stdio.h>
#include termios.h>
#include unistd.h>
#include sys/types.h>
#include sys/time.h>
/* This id to hide the character we type and suspend any
prints after dir 1 , displayed ony when dir 0 */
void changemode ( int dir )
{
static struct termios oldt, newt;
if ( dir == 1 )
{
tcgetattr ( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~ ( ICANON | ECHO );
tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
}
else
tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
}
int kbhit ( void )
{
struct timeval tv;
fd_set rdfs;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO ( &rdfs );
FD_SET ( STDIN_FILENO, &rdfs );
// select ( STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv );
select ( STDIN_FILENO + 1, &rdfs, NULL, NULL, NULL );
printf(" After ");
return FD_ISSET ( STDIN_FILENO, &rdfs );
}
int main ( void )
{
int ch;
printf(" Press any Key to continue\n ");
changemode ( 1 );
while ( !kbhit() )
{
// putchar ( '.' );
}
ch = getchar();
printf ( "\nGot %c\n", ch );
changemode ( 0 );
return 0;
}
Well I would just use ncurses, but you could possibly use below? Chances are you problly don't need either unless you're making a crappy console game, or a password program.
*/
#include stdio.h>
#include termios.h>
#include unistd.h>
#include sys/types.h>
#include sys/time.h>
/* This id to hide the character we type and suspend any
prints after dir 1 , displayed ony when dir 0 */
void changemode ( int dir )
{
static struct termios oldt, newt;
if ( dir == 1 )
{
tcgetattr ( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~ ( ICANON | ECHO );
tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
}
else
tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
}
int kbhit ( void )
{
struct timeval tv;
fd_set rdfs;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO ( &rdfs );
FD_SET ( STDIN_FILENO, &rdfs );
// select ( STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv );
select ( STDIN_FILENO + 1, &rdfs, NULL, NULL, NULL );
printf(" After ");
return FD_ISSET ( STDIN_FILENO, &rdfs );
}
int main ( void )
{
int ch;
printf(" Press any Key to continue\n ");
changemode ( 1 );
while ( !kbhit() )
{
// putchar ( '.' );
}
ch = getchar();
printf ( "\nGot %c\n", ch );
changemode ( 0 );
return 0;
}
Monday, January 7, 2008
getche() equivalent in Linux
]# echo -n "Press any key to continue..." && read -n 1
or
]# read -sn1 -p " Press any key to continue "
Also :
]$ echo -n "Press any key to continue..." && /sbin/getkey
##test.sh
if /sbin/getkey -i -c 5 -m "Press Y within %d sec to continue " y;
then
echo You pressed y
else
echo You pressed Wrong
fi
echo
/sbin/getkey i && touch /var/run/confirm
if pressed key is 'i' return 1 else 0.
&& fail if 0 .
http://www.webservertalk.com/archive200-2005-7-1138374.html
or
]# read -sn1 -p " Press any key to continue "
Also :
]$ echo -n "Press any key to continue..." && /sbin/getkey
##test.sh
if /sbin/getkey -i -c 5 -m "Press Y within %d sec to continue " y;
then
echo You pressed y
else
echo You pressed Wrong
fi
echo
/sbin/getkey i && touch /var/run/confirm
if pressed key is 'i' return 1 else 0.
&& fail if 0 .
http://www.webservertalk.com/archive200-2005-7-1138374.html
Friday, January 4, 2008
Advanced Macro Tricks (Tokenizer )
Pasting Tokens
================
Each argument passed to a macro is a token, and sometimes it might be expedient to paste arguments together to form a new token. This could come in handy if you have a complicated structure and you'd like to debug your program by printing out different fields. Instead of writing out the whole structure each time, you might use a macro to pass in the field of the structure to print.
To paste tokens in a macro, use ## between the two things to paste together.
For instance
#define BUILD_FIELD(field) my_struct.inner_struct.union_a.##field
Now, when used with a particular field name, it will expand to something like
my_struct.inner_struct.union_a.field1
The tokens are literally pasted together.
String-izing Tokens
====================
Another potentially useful macro option is to turn a token into a string containing the literal text of the token. This might be useful for printing out the token. The syntax is simple--simply prefix the token with a pound sign (#).
#define PRINT_TOKEN(token) printf(#token " is %d", token)
For instance, PRINT_TOKEN(foo) would expand to
printf("foo" " is %d" foo)
(Note that in C, string literals next to each other are concatenated, so something like "token" " is " " this " will effectively become "token is this". This can be useful for formatting printf statements.)
For instance, you might use it to print the value of an expression as well as the expression itself (for debugging purposes).
PRINT_TOKEN(x + y);
http://www.cprogramming.com/tutorial/cpreprocessor.html
================
Each argument passed to a macro is a token, and sometimes it might be expedient to paste arguments together to form a new token. This could come in handy if you have a complicated structure and you'd like to debug your program by printing out different fields. Instead of writing out the whole structure each time, you might use a macro to pass in the field of the structure to print.
To paste tokens in a macro, use ## between the two things to paste together.
For instance
#define BUILD_FIELD(field) my_struct.inner_struct.union_a.##field
Now, when used with a particular field name, it will expand to something like
my_struct.inner_struct.union_a.field1
The tokens are literally pasted together.
String-izing Tokens
====================
Another potentially useful macro option is to turn a token into a string containing the literal text of the token. This might be useful for printing out the token. The syntax is simple--simply prefix the token with a pound sign (#).
#define PRINT_TOKEN(token) printf(#token " is %d", token)
For instance, PRINT_TOKEN(foo) would expand to
printf("foo" " is %d" foo)
(Note that in C, string literals next to each other are concatenated, so something like "token" " is " " this " will effectively become "token is this". This can be useful for formatting printf statements.)
For instance, you might use it to print the value of an expression as well as the expression itself (for debugging purposes).
PRINT_TOKEN(x + y);
http://www.cprogramming.com/tutorial/cpreprocessor.html
Thursday, January 3, 2008
Vb Script to automate the mail
Vb Script to automate the mail
------------------------------
SeeGood.vbs
-----------
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "Outlook"
WScript.Sleep 2500 ' Comment : Give Notepad some time to load
WshShell.SendKeys "^{n}" ' Ctrl+N
WScript.Sleep 1000
WshShell.SendKeys "test@mailid.com"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WScript.Sleep 100
WshShell.SendKeys " Hi,I would have checked out the code first! ....."
WshShell.SendKeys "{ENTER}"
WScript.Sleep 5000
WshShell.SendKeys "%{s}" 'Alt+s
'For
'SHIFT = +
'CTRL = ^
'ALT = %
------------------------------
SeeGood.vbs
-----------
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "Outlook"
WScript.Sleep 2500 ' Comment : Give Notepad some time to load
WshShell.SendKeys "^{n}" ' Ctrl+N
WScript.Sleep 1000
WshShell.SendKeys "test@mailid.com"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WshShell.SendKeys "{TAB}"
WScript.Sleep 100
WshShell.SendKeys " Hi,I would have checked out the code first! ....."
WshShell.SendKeys "{ENTER}"
WScript.Sleep 5000
WshShell.SendKeys "%{s}" 'Alt+s
'For
'SHIFT = +
'CTRL = ^
'ALT = %
Subscribe to:
Posts (Atom)