LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Blogs > rainbowsally
User Name
Password

Notices

Rate this Entry

Sockets -- A Chat Server with a non-blocking 'select' (i.e., GUI ready)

Posted 01-27-2013 at 06:47 AM by rainbowsally
Updated 09-04-2013 at 08:50 PM by rainbowsally (fix bad edit)

Sockets -- A Chat Server with a non-blocking 'select' (i.e., GUI ready)

[GUI-ready but no GUI in this version. Terminal only.]

Today's feature is
  • A chat server.
  • Non blocking 'select' (which we need for importing into an lqMiniTerm).
  • A web link to more generic sockets code that I wish I had found earlier.
  • Tarred up example code with makefile for all the code examples at bgnet.

Here's a chat-server you can use to communicate between folks on your computer (or more likely your own self) that demonstrates using non-blocking 'select' for use in a gui loop. The original code and lots of great info is from here.

http://beej.us/guide/bgnet (See bottom of this blog entry if you want to d/load all the examples and a makefile)

I wish I had found that page first. Could have avoided a lot of problems in the netdog. :-)

The main difference between this code and the code at bgnet is that we can't 'block' in our application (ready to put into an lqMiniTerm terminal) so we use a timeval struct set to zero to just cruise through and see if anyone has joined (sets the listener's fd) or posted anything (sets one of the others).

The non-blocking also requires us to record the operations to run in each loop. These are called the 'run_state' in our applications but they are also equivalent to 'goto's and byte codes.

If that last part of the last sentence threw you, some interpreters execute byte codes in a case switch. Each bytecode is a case value. Compiling jump table indexes or compiling literal jump addresses is much more efficient and for speed, most interpreters no longer use case switches but the relationship between our 'run_state's and an interpreter's run loop is only a matter of 1) degree, 2) where the case IDs come from and 3) how the jump offset is derived, which (again) may be a case switch, an index into a jump table, or a literal jump address.

Back to the chat server.

Once again, we're going with a self-extractor and the reason for that is because I changed the compile flags and your best chance of getting this to compile is with the makefile.

It includes debug info but it's also set for 'gcc -std=gnu99' which is NOT the same as '-std=c99', so... for our friends that don't have mc2, we'll go the extra mile at this end.

:-)

That flag just allows defining the loop index inside a for(..) loop.

See http://beej.us/guide/bgnet for more generic code examples and lots of info on why things are done the way they are and grab the d/load at the bottom if you want a makefile for all the examples.

Thanks Beej! :-)

file: chat-server-files.sfxz
purpose: utility (executable)
Code:
#!/bin/sh
# base64 self extractor
# file: chat-server-files

filename=chat-server-files

# don't allow user to click on the file
if ! tty >/dev/null; then
  xmessage -center "
  $(basename $0)
  must be run in a terminal 
  "
  exit
fi

# create and/or clear out tmp dir
mkdir -p $HOME/tmp/sfxz
rm -rf $HOME/tmp/sfxz/*
base64 -d << _BEOF >$HOME/tmp/sfxz/$filename.xz
/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4E//DgJdABcLyOdpie6hiaROxEHxnnVKNdSfgC6p0ucd
f8yjhePXvcj5TTsTJ5vxyTw6CnIEnyLvLYPIPG/X0RwaHrt4lq97TN9fxgMc1IOHVj9xE3tkeptN
he6dsC+oVnSQOCETyjqJGZNnVJBxJATDB6ISi2SHzojJPYd+MEjPMguN9hIIFxh8C+yBifcpmwA7
9Yc+7Q036YUl7wXwmTSQ/TduhpyVr4udYI4uxo3PRjMt2084VZ6cyza2WS+IWIyK8ozQdU0+mQZh
U/V6DYRcKkzdAfVVjWRr/MrPefURnYemc/x1KQjTO3bMHNzsGVrgGFSVGCoRMfKnDV3QRnnWEF8h
bWEPlOHRJ1nVQmpTBHGl4QxVUZmspURhGECM02llOjOYXDBrKgDlcz+24ej4CeeCgqSNbMuTChxI
2dx2VLsFet1LkCELBc4zePg87AJ7OQqOsviJWS8Jwgye5CiYwA6NruoH4EzoBQhE6fMas6iGB/Qq
i0TpMUYs+Fg6QRL3NB4gzPfa8ZXdK4ai2gYOCYBeNnxdJO4uq4QCegvjHlZkbIaxK6oQ8t28lfMS
7lL5sGivJhlakSABeEMTjD82h+CwNrGMPiO+5xfWKvKx4kWg66MdEINPCK9TtZvfqRYbyWve4dDe
PbvSzXB6G6d22iiC6VcyGXfJpAWrPqxxriHYtTfzhhvAMjI+l3/UNdx3jx+3YFECMnKFgAnttzcI
OkU6k2sveJL4dqFuoOK3OcOutog8YcG/8ZNDqZtilTXgaMrdeWT2TcOiiUQYQ2N1lasF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PKXl4U2Au4/yZ2qTrLaZ7pOzb
310Fi4dtD7jpDCQYvsgcDOEkXB7x9inn3+xfGhhcr0ue+n5JnzFBnY8SIwSezPmm23CheL5BaNRR
AFnJ8zqyAiauKB30XhtgSOWLpaQpJzOFIuEIgSuRmh6CI4VMm2wuGtlsu3ocJXvkfYqRPzeEh5g+
xSfLJDO17awm9HwnxeJfJ/dL2yEHCjbB975vmqSTacMvd8amAagEdGj7yx2TBPTjSUK3gvLuulws
YYHVRdhhPe65GuNrl/cW0ZgLy7MRGZTbI2eXUDkalMPUTOA0PORGYaGlkhsfRfGMmoWqtciQdKH5
RNOiGou/FeSzV2hZZKz4aZfq1tIj6/6j8ZBlYlMfO2Y1tz1wLTtfGicVDdmvcBcw4WAcpHRc2DSl
w3TklknpMVDGuOCYjVEt+3uOdCGjKF5q4m7T5Mjh8fMI8ctwNP2cOBNVd/HklveqRXd1AW6Ww826
pMex4qVz5qQIU0XziJOkJPxyxo4LrYjH+iqP38SXBtmVvvNLY6dltyQTqew/WzaGcJbkCuD3fFV7
RlfVlRitOKHSnGVyCImhRzzK65TDmQqDavn3plHrKG2DEjgel1TzlcQ8klTsvQ6d93VFbAy/fLr4
4ZFobkVoIKZnpyT/64/w5kUPrZVDNTxp/q7ECeuU3Uuo9m1Nch7hXujky0hWk1hkavCjrbFRPzLQ
w2bm04htTmIJwv7aqx3EtxEvv1SKluuNEzTkEDeipur9uf87SGos6Ib/HHo6KV58W+TojoWbRezc
E8sm82qwghNm4j0XDIJNvjQBfN6nLIRN3AAAAAAigtZsSQCbAAABnhyAoAEAdwMTbLHEZ/sCAAAA
AARZWg==
_BEOF

(cd $HOME/tmp/sfxz && tar -xaf chat-server-files.xz)
rm -f $HOME/tmp/sfxz/chat-server-files.xz
 
cat << _BEOF > $HOME/tmp/sfxz/post-extract
#!/bin/sh

is_yes() # returns OK if first char is Y or y
{
  local key=`echo $1 | cut -b1 | sed 's/y/Y/; /Y/!d'`
  [ "$key" == 'Y' ] && return 0 # true
  return 1 # false
}

if [ -e chat-server-files ]; then
  printf "
  Overwrite existing file(s)? [N/y]:"
  read key
  if ! is_yes $key ;then
  echo "Aborting.."
    exit 0
  fi
fi

mv $HOME/tmp/sfxz/chat-server-files .
_BEOF

sh $HOME/tmp/sfxz/post-extract
rm -rf $HOME/tmp/sfxz/*
rmdir $HOME/tmp/sfxz 2>/dev/null || true
rmdir $HOME/tmp 2>/dev/null || true
Run the self extractor to unpack the directory and files, ready to 'make'.

You'll probably need telnet to log in. And you probably won't be able to use the same terminal for the server and the client(s). If you get 'connection refused' errors, see if you can't find another way to open a terminal. You do NOT need root perms for this or any other app I share here unless it's clearly marked as such.

Also, if you want ALL of beej's examples with a makefile (and mc2.def) to create the examples with debug info (great with kdbg), here's a little d/load I packed up for you.

Here's all the examples and a makefile to build 'em.

http://rainbowsally.net/rainbowsally...xamples.tar.gz

Took about a minute to generate the makefile and test compile.
Code:
mc2 -fetch c-32   # 32 bit straight C
#edited mc2.def:
# changed OUTNAME to MULTI so each source file creates an executable
# changed SRCDIR to '.' so we don't need the extra src dir 
# changed to debug build so we can trace the code and see the variables
make update       # generated the makefile
make              # created 11 executables
Maybe less than a minute. But... It's up to you. There's always automake, CMake, IMake, QMake, ...

mc2 is not meant to replace those apps but it's for when you don't NEED them. If you're curious, try this. It installs and builds in your own HOME (a sandbox) under ~/usr32.
http://www.linuxquestions.org/questi...support-34783/

- The Computer Mad Science Team

:-)
Posted in Uncategorized
Views 598 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 11:23 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration