Help with simple C chat + Kqueue

  • Thread starter James889
  • Start date
  • Tags
    Chat
In summary, there are a few steps you need to follow in order to use kqueue() for your server/client chat. These include creating a kqueue object, registering the socket descriptor, waiting for events with kevent(), and handling the events that have been triggered.
  • #1
James889
192
1
Hello,

I'm writing a very simple server/client chat. At the moment i have no way of determining when data arrives in the receive buffer.

This is how the send/receive part of the chat currently looks like:

Code:
.
.
.
for(;;){

fgets(sendbuffer,sizeof sendbuf,stdin);

    send(acceptfd,sendbuffer,sizeof sendbuffer,0);

         recv(socketfd,recvbuff,sizeof recvbuff,0);
printf("%s",recvbuff);

}

This is very primitive, i would like to use kqueue() to decide when data is available on the listening socket. So i looked at a few examples of how to use kqueue but i didn't get it working properly. This is what i tried..
Code:
struct kevent events;
struct kevent changes;

int new_event;

EV_SET(&changes,sockfd,EVFILT_READ,EV_ADD | EV_ENABLE,0,0,0);

for(;;){

new_event = kevent(kq,&changes,1,&events,1,NULL);  

if(events.flags & EVFILT_READ){
// call recv..
}

else ...
//call fgets and send 
}

unfortunately this didn't work out very well, i still have to press return for reading and printing data from the buffer.

Any suggestions?
 
Physics news on Phys.org
  • #2
Thanks!</code>There are a few things that you need to do in order to get your kqueue code to work properly. First, you need to create the kqueue object with the kqueue() system call. Second, you need to register the socket descriptor with the kqueue() system call. This is done with the EV_SET macro, which is what you have done in your code. Third, you need to use the kevent() system call to wait for events to happen on the registered descriptors. This will return an array of events that have happened. Finally, you need to handle the events that have been triggered. You can do this by checking the flags field of the kevent structure. For example, if you are waiting for socket events, you can check the flags field for EVFILT_READ and EVFILT_WRITE. If the flag is set for EVFILT_READ, then you can read the data from the socket. Similarly, if the flag is set for EVFILT_WRITE, then you can write data to the socket. I hope this helps!
 

1. What is "Help with simple C chat + Kqueue"?

"Help with simple C chat + Kqueue" refers to assistance with implementing a simple chat program using the C programming language and the kqueue event notification mechanism. This involves creating a basic chat server and client that can communicate with each other over a network.

2. Why use C and Kqueue for a chat program?

C is a popular and powerful programming language that allows for efficient and low-level network programming. Kqueue is a scalable and high-performance event notification mechanism that can handle large numbers of connections. Together, they provide a solid foundation for building a reliable and fast chat program.

3. What is the purpose of a chat program?

The purpose of a chat program is to allow users to communicate with each other in real-time over a network. This can be used for various purposes such as online messaging, group discussions, or collaborative work. A chat program can also be a fun and convenient way to stay connected with friends and family.

4. Is prior knowledge of C and Kqueue necessary to get help with this project?

Yes, some prior knowledge of C and kqueue is necessary to understand and implement a simple chat program using these technologies. However, there are many resources available online that can help beginners get started with these concepts.

5. Can I customize the chat program to fit my specific needs?

Yes, since you will be writing the code for the chat program, you can customize it to fit your specific needs. This can include adding new features, changing the user interface, or modifying the functionality. However, it is important to have a good understanding of the code and its implications before making any changes.

Similar threads

  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Quantum Physics
Replies
16
Views
2K
  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
2
Views
13K
  • Programming and Computer Science
Replies
2
Views
2K
  • STEM Academic Advising
Replies
10
Views
1K
Replies
165
Views
21K
Back
Top