WinSock Client C - c

I have a client using select() to check if there is anything to be received, else it times out and the user is able to send(). Which works well enough. However, the program locks up waiting for user input, so it cant recv() again until the user has sent something.
I am not having much luck with using threads either, as I cannot seem to find a good resource, which shows me how to use them.
I have tried a creating two threads (using CreateThread) for send and recv functions which are basically two functions using while loops to keep sending and receiving. And then the two CreateThreads() are wrapped up in a while loop, because otherwise it just seemed to drop out.
I have basically no previous experience with threads, so my description of what i've been doing will probably sound ridiculous. But I would appreciate any help, in using them properly for this kind of use, or an alternative method would also be great.

Can't find a good resource on socket programming? Bah. Read the bible:
Unix Network Programming

this has a strong feel of assignment work...
I am not too much into Windows programming but when I did do some earlier, there used to be a kbhit (probably) function that allowed you to check if the user had sent any inputs. You could look for something similar and try a non-blocking user input check before starting your select again.
You could then get this working without multi-threading
(unless, er, you have to use that).

Related

Poll with changing amount of file descriptors

In my program UDP sockets are stored in a limited size array, socke can be removed from the array which results in a restructuration of the array to keep it sequential.
I wan't to create a thread waiting for a certain message on those sockets and I would like to use non-blocking I/O for that task.
My problem is that the array of socket can change over the time, using poll if I wan't to take those changes in account I need to modify the array of pollfd. But, if poll is already called how can I make it take those changes into account?
I still could use a timeout but I find that solution not really good because it's kind of like a "non satisfying receptor" (I hope it's the right word in english).
Thank you in advance

Can I get socket buffer remainder size?

I have a real-time system, so I using the non-blocking socket to send my data
But there is happened that the socket buffer is full,
so the send function's return value less than my data length.
If I save the return length and re-send, there is not different with blocking socket?
So can I get the socket buffer's remainder size? I can check it first,
if it is enough then I call send, skip send else.
Thank you, all.
Well there is a difference between blocking and non-blocking - if you experience a short write you don't block. That's the whole point of non-blocking. It give you an opportunity to do something more pressing while waiting for some buffer space to free up.
Your concern seems to be the repeated attempts to write a full message, that is, a form of polling. But a check of the bytes free in the buffer is the same thing, you are just substituting the call to the availability with the call to write. You really don't gain anything efficiency wise.
The commonplace solution to this is to use something like select or poll that monitors the socket descriptor for the ability to write (and least some) bytes. This allows you stop polling and foist some of the work off on the kernel to monitor the space availability for you.
That said, if you really want to check to see how much space is available there are usually work arounds that tend to somewhat platform specific, mostly ioctl calls with various platform specific parameters like FIONWRITE, SIOCOUTQ, etc. You would need to investigate exactly what your platform provides. But, again, it is better to consider if this is really something you need in the first place.
If the asynchronous send fails with EWOULDBLOCK/EAGAIN, no data is sent. You could then try to send something else, or wait until the buffer is free again.
Also see https://stackoverflow.com/questions/19391208/when-a-non-blocking-send-only-transfers-partial-data-can-we-assume-it-would-r - a related issue is discussed there.

C function that doesn't 'wait' for any input, but detects if any?

Is there a C function that doesn't wait for input but if there is one, it detects it?
What I'm trying to do here is continue a loop endlessly until any key is pressed.
I'm a newbie, and all the input functions I've learned so far waits for the user to input something..
I hope I'm clear, although if I'm not I'm happy to post the code..
WIndows kbhit( ) does exactly this non-blocking keyboard char-ready check, and there's a kbhit( ) for Linux over here
Since nobody's stated it clearly....
The important thing to note is that the standard library provided by C does not provide the capability you're looking for. Achieving it, then, requires the use of third party libraries and/or special knowledge about the operating system you're using.
Typically, you'll have some of those third-party libraries available. If you were using Visual Studio, for example, you would be able to use http://msdn.microsoft.com/en-us/library/58w7c94c(v=VS.100).aspx. I'm not sure what's available to you with your setup.
you should use select or poll
You might also want to check signal() if all you need is a way to stop the loop and run your end of the program function.
It depends what you exactly want to do, but in general:
A) You keep your program single-threaded and check input through a non-blocking input read.
B) You spawn a different thread that will handle the input and communicate the results back to the main thread.

How to trap read write system calls?

Whenever i attempt to write anything on my pendrive, a write system call is generated. What i want to do is, this write call should be trapped and and the user should be requested to input predecided password( which i can define during coding itself).
Please tell me whether this is possible or not? and if yes than how should i do it?
The windows DDK has an example of hooking the file reads/writes/copies in filesys\minifilter, with both pre and post op callbacks, that should have you set for the kernel side of things. For the gui part you'll need something to do a non-blocking spin till the drives signals an event, you'll probably also want a pipe or mapped memory view to pass data around
EasyHook is supposed to give you the ability to hook kernel functions. I have not tried it, so your mileage may vary. Be sure to hook functions cautiously - you may degrade the performance of your machine to a point where it's unusable. What you want is to interact with the user, meaning that you must put the hooked function on hold, and issue a callback into user space. This is probably not an exercise for mere mortals.
At any rate, good luck!

Difficulties displaying the text in simple client/server chat program (current input gets displayed) - C

I am writing a chat program for my networking class and I have all the networking setup perfectly.
My problem is if a client is currently writing a message and he receives a message from a different client then his current input gets displayed with the received message.
For example if a client is writing a message "Hi there how are you?" and receives a message "Good day to you!" while in the middle of writing their message it gets displayed as:
Hi there hoGood day to you!
->w are you?
Where -> is the area for the user to type in the message. What I would like to happen is to just display the message received and have the area -> retain all the previous text that was written before the message was received.
Please make note that what the client is typing in is still in fact "there" when he receives a message. If he completes his message his full message will be sent.
Also note that my client uses pthreads. One thread to read messages from the server and display them to the users screen and one thread to read from stdin and send the messages to the server. I do believe that my problem is arising because I am using pthreads and the threads share the same stdin, stdout, stderr. Maybe this is a misconception and wrong?
I hope I have been clear on my problem. If not, sorry. Please let me know what I can clarify for you.
I started doing some research and came upon these links:
ANSI Escape Characters
Thread from Stackoverflow
I was thinking about trying to go up lines and move the cursors around and stuff, but don't know if that is the most effective way to do so. Firstly because I don't know how to capture the information that is in the terminal waiting to "entered"/sent to stdin. Maybe I just haven't found out how to do that.
Also I was wondering if there was a way to work/manipulate file descriptors to solve the problem? Maybe that wouldn't even solve it?
Thanks for reading and your time. I appreciate all your help.
Using a library such as curses to manage text 'windows' will be easier than trying to manipulate the screen by hand.
I am not an expert in unix network programming, but I am pretty much convinced that the problem is with multithreading itself rather than some stdin/stdout quirks.
What I see here is multiple threads accessing the same resource (terminal session) without any synchronization. This inevitably leads to race conditions between them.
I would recommend you to read this free e-book on sychronization problems, which is especially helpful for those who are only slightly familliar with sychronization:
http://www.greenteapress.com/semaphores/
Designate a thread as the IO thread and sent the messages to be displayed to that thread through a blocking queue (or circular buffer). Does C have those? (I use Java currently).
The problem involves threading. Your solutions are to either use one display and block the incoming message until the user finishes with the current input or use two "windows". Many conversation programs have two windows: one for incoming data (or the current conversation) and another to build the next message.
The standard C language does not have facilities for threading, windowing or cursor positioning. You'll just have to use platform specific features. Since you didn't specify your platform, you will have to look these up yourself.
By default user input is handled by the terminal itself, so a mutex alone wouldn't cut it if you want real-time updates. If you wanted a line-input mode solution you could log incoming messages and commit them every time a message is sent and before the next one is read.
Else, your best bet would be using curses as suggested. A scrollok(3x) enabled window can be used like a terminal easily using waddstr(3x) and wgetnstr(3x), no need to micromanage that if you use an IRC-like UI.
Note that using curses doesn't mean you don't have to use a mutex around your curses functions. Else, when you less expect it the screen will become full of garbage.

Resources