taron_31 | Hello all,
j'ai commencé a programmer un messenger en C avec la bibliothèque GTK+, le problème est que le code ne fonctionne pas (je ne sais pas vraiment a quel niveau), mais il se compile. Je n'arrive pas tellement a vous expliquer mon problème, moi même j'ai du mal a comprendre, ça doit surement venir de code, le voici :
Code :
- /* Main C file */
- #include <stdio.h>
- #include <gtk/gtk.h> /* Headers */
- #include <winsock2.h>
- #include "infos.h" /* Infos about Alaum product */
- #define MAX_RECV 512
- #define NO_SENT "Your message has not been sent !"
- #define SENT "Your message has been sent !"
- #define PORT_DEFAULT 4666
- #define IP_DEFAULT "127.0.0.1"
- #define CONNECTED 0
- #define ABORTED 1
- /* Prototypes functions */
- short socket_create(); // Create 2 socekt (server & client)
- void OnEnter_Send(GtkWidget *, gpointer); /* Callbacks for GTK+ */
- void OnClick_Send(GtkWidget *, gpointer);
- GtkWidget *pLabel_Mess, *pLabel_StateFriend;
- SOCKADDR_IN server_sockad, client_sockad, friend_sockad; // sockaddr_in objects declaration
- int socket_server, socket_client, friend_socket, friend_size; // socket description declaration
- int main(int argc, char *argv[])
- {
- /* Widgets declaration */
- GtkWidget *pTable, *pVBox, *pVBox_Frame,*pWindow;
- GtkWidget *pExit_Button, *pSend_Button, pRefresh_Button, *pEntry_ToSend, *pFrame;
-
- char buffer_recv[MAX_RECV];
-
- gtk_init(&argc, &argv); /* GUI (gtk+) creating */
-
- pTable = gtk_table_new(10, 3, TRUE); // Create table
-
- pVBox = gtk_vbox_new(TRUE, 0); /* Create box */
- pVBox_Frame = gtk_vbox_new(TRUE, 0);
- pWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL); /* Window initilization */
- gtk_window_set_title(GTK_WINDOW(pWindow), PRODUCT);
- gtk_window_set_default_size(GTK_WINDOW(pWindow), 250, 280);
-
- pExit_Button = gtk_button_new_with_label("Exit" ); /* Create buttons */
- pSend_Button = gtk_button_new_with_label("Send" );
-
- pLabel_Mess = gtk_label_new("Welcome to Alaum !" );
- pLabel_StateFriend = gtk_label_new("" );
-
- pEntry_ToSend = gtk_entry_new(); /* New Entry */
- gtk_entry_set_text(GTK_ENTRY(pEntry_ToSend), SENT);
-
- pFrame = gtk_frame_new("Information" ); /* Create frame */
- gtk_frame_set_shadow_type(GTK_FRAME(pFrame), GTK_SHADOW_OUT);
-
- gtk_box_pack_start(GTK_BOX(pVBox_Frame), pLabel_StateFriend, TRUE, TRUE, 10);
-
- gtk_box_pack_start(GTK_BOX(pVBox), pFrame, TRUE, FALSE, 0); // Write frame to wbox
- gtk_container_add(GTK_CONTAINER(pFrame), pVBox_Frame); // Include vbox_frame to Frame
-
- /* Attach Widgets at Table */
- gtk_table_attach(GTK_TABLE(pTable), pVBox, 0, 4, 4, 6, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
- gtk_table_attach(GTK_TABLE(pTable), pExit_Button, 3, 4, 9, 10, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
- gtk_table_attach(GTK_TABLE(pTable), pSend_Button, 0, 1, 7, 8, GTK_EXPAND | GTK_FILL, GTK_EXPAND, 0, 0);
- gtk_table_attach(GTK_TABLE(pTable), pEntry_ToSend, 0, 4, 6, 7, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
- gtk_table_attach(GTK_TABLE(pTable), pLabel_Mess, 0, 4, 8, 9, 0, 0, 0, 0);
-
- /* Connect signals at callback functions */
- g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
- g_signal_connect(G_OBJECT(pEntry_ToSend), "activate", G_CALLBACK(OnEnter_Send), pLabel_Mess);
- g_signal_connect(G_OBJECT(pSend_Button), "clicked", G_CALLBACK(OnClick_Send), pTable);
- g_signal_connect(G_OBJECT(pExit_Button), "clicked", G_CALLBACK(gtk_main_quit), NULL);
-
- gtk_container_add(GTK_CONTAINER(pWindow), pTable); // Add Table to main window
-
- gtk_widget_show_all(pWindow); /* Start the GUI : finish ! :-) */
- gtk_main();
-
- socket_create();
-
- friend_size = sizeof(friend_sockad);
-
- bind(socket_server, (SOCKADDR *)&server_sockad, sizeof(server_sockad));
- listen(socket_server, 0);
-
- // Check if friend is connected, if not wait connection
- if(connect(socket_client, (SOCKADDR *)&client_sockad, sizeof(client_sockad)) < 0)
- {
- MessageBox(0, "Your friend is not connected", "Informtion", MB_ICONINFORMATION);
- friend_socket = accept(socket_server, (SOCKADDR *)&friend_sockad, &friend_size);
- }
-
- send(friend_socket, "Hello !", sizeof("Hello !" ), 0);
- recv(socket_server, buffer_recv, MAX_RECV - 1, 0);
-
- closesocket(socket_server); /* Closing socket */
- closesocket(friend_socket);
-
- return 0; /* Alaum's end ;-( */
- }
- void OnEnter_Send(GtkWidget *pEntry, gpointer Data)
- {
- const gchar *pText; // Text from pEntry
-
- pText = gtk_entry_get_text(GTK_ENTRY(pEntry));
-
- if(send(friend_socket, pText, sizeof(pText), 0) < 0) // Try to send pText
- {
- gtk_label_set_text(GTK_LABEL(pLabel_Mess), NO_SENT); // Modify label's text
- return;
- }
-
- gtk_label_set_text(GTK_LABEL((GtkWidget *)Data), SENT); // Modifiy label's text
- }
- void OnClick_Send(GtkWidget *pButton, gpointer Data)
- {
- const gchar* pText; // Text from pEntry
-
- GtkWidget *pEntry, *pLabel; // Widget declarations
- GList *pList = 0; // List
-
- pList = gtk_container_get_children(GTK_CONTAINER(Data)); // Get linked list
-
- pList = g_list_next(pList); // Next element
- pEntry = GTK_WIDGET(pList->data); // Get Entry Widget
-
- pText = gtk_entry_get_text(GTK_ENTRY(pEntry)); // Get entry's text
-
- send(friend_socket, pText, sizeof(pText), 0);
- g_list_free(pList); // Free attitude !
- }
- short socket_create()
- {
- int reply;
-
- WSADATA wsaData;
- reply = WSAStartup(MAKEWORD(2, 0), &wsaData);
-
- /* Add WSAStartup()'s chek here !! */
-
- /* Create socket */
- if((socket_client = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
- {
- MessageBox(0, "Error", "Error while socket creating... aborted !", MB_ICONWARNING);
- return ABORTED;
- }
-
- client_sockad.sin_addr.s_addr = inet_addr(IP_DEFAULT); /* Socket's initialization */
- client_sockad.sin_port = htons(PORT_DEFAULT);
- client_sockad.sin_family = AF_INET;
-
- /* Create socket */
- if((socket_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET )
- {
- MessageBox(0, "Error", "Error while socket creating... aborted !", MB_ICONWARNING);
- return ABORTED;
- }
-
- server_sockad.sin_addr.s_addr = inet_addr(IP_DEFAULT); /* Socket's initialization */
- server_sockad.sin_port = htons(PORT_DEFAULT);
- server_sockad.sin_family = AF_INET;
-
- WSACleanup();
- return CONNECTED;
- }
|
Plus exactement, le problème est que la connexion ne se réalise pas entre 2 programmes...
Pouvez-vous m'éclaircir un peu s'il vous plaît ?
Merci |