Use GTK and socket in Linux to implement simple chat rooms

Source: Internet
Author: User
Article title: use GTK and socket in Linux to implement simple chat rooms. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

When you use GTK and socket to implement a simple chat room, a "segment error" occurs ". Where is the problem ??

Please check out my program. I will use the GTK graphic interface as a simple chat room to implement communication on two machines. Now the program can be compiled, but when running, when the customer segment pop-up logon interface, enter the user name and click OK, the "segment error" will be displayed on the terminal ". I am a newbie. I am just getting started. I hope to reply to my predecessors.

Server:

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include


# Include

# Include

# Include

# Include

# Include

# Define OUTPORT 3333
# Define MAX_USERS 8

Struct _ client {
Gint sd;
Gboolean in_use;
Gchar name [64];
Gchar buf [1024];
};
Typedef struct _ client;

Client user [MAX_USERS];

Void do_service (gpointer id)
{
Gint j;
Char tobuf [1024];

While (read (user [GPOINTER_TO_INT (id)]. sd,
User [GPOINTER_TO_INT (id)]. buf, 1024 )! =-1)
{
Sprintf (tobuf, "% s: % s \ n", user [GPOINTER_TO_INT (id)]. name,
User [GPOINTER_TO_INT (id)]. buf );
For (j = 0; j {
If (user [j]. in_use)
{
Write (user [j]. sd, tobuf, 1024 );
G_printf ("% s", tobuf );
}
}
}
User [GPOINTER_TO_INT (id)]. in_use = FALSE;
Close (user [GPOINTER_TO_INT (id)]. sd );
// Exit (0 );
}

Int main (int agrv, char * argv [])
{
Gint sd, newsd;
Struct sockaddr_in * my_addr;/* Local address information */

Struct sockaddr_in * remote_addr;/* client address information */
Gint slen;
Gint count = 0;
Gint flags;
Gchar buf [1024];
Gchar tobuf [1024];
Gint length, I, j;

If (! G_thread_supported ())
G_thread_init (NULL );
Else
G_print ("thread not support \ n ");

Sd = socket (AF_INET, SOCK_STREAM, 0 );
If (sd =-1)
{
G_print ("Creat socket error! \ N ");
Return-1;
}

My_addr = g_new (struct sockaddr_in, 1 );
My_addr-> sin_family = AF_INET;
My_addr-> sin_port = htons (OUTPORT );
My_addr-> sin_addr.s_addr = INADDR_ANY;
Bzero (& (my_addr-> sin_zero), 8 );

Slen = sizeof (struct sockaddr_in );

If (bind (sd, (struct sockaddr *) my_addr, slen) <0)
{
G_print ("bind error \ n! ");
Return-1;
}

If (listen (sd, 8) <0)
{
G_print ("listen error! \ N ");
}

For (I = 0; I user. in_use = FALSE;

Flags = fcntl (sd, F_GETFL );
Fcntl (sd, F_GETFL, flags &~ O_NDELAY );

For (;;)
{
Newsd = accept (sd, (struct sockaddr *) remote_addr, & slen );
If (newsd =-1)
{
G_print ("accept error \ n ");
Break;
}
Else
{
If (count> = MAX_USERS)
{
Sprintf (buf, "too many users, the server cannot be connected! \ N ");
Write (newsd, buf, 1024 );
Close (newsd );
}
Else
{
Flags = fcntl (user. sd, F_GETFL );
Fcntl (user. sd, F_SETFL, O_NONBLOCK );
User [count]. sd = newsd;
User [count]. in_use = TRUE;
Read (newsd, user [count]. name, 64 );

G_thread_create (GThreadFunc) do_service,
(Gpointer) count, TRUE, NULL );
Count ++;
}
}
} // (;;)

Close (sd );
G_free (my_addr );
}

[1] [2] Next page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.