/*
Then save this file as window.c and compile it using this command
(those are backticks, not single quotes):
gcc -Wall -g -o window window.c `pkg-config --cflags --libs gtk+-2.0`
Then execute it using:
./window
*/
#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include "wrap.h"
#define MAXLINE 1000
#define SERV_PORT 8000
int sockfd, n;
GtkWidget *textview2;
pid_t pid;
pthread_t ntid;
static pthread_t tid;
int err;
static int retvalue=0;
char buf[MAXLINE];
void *thr_fn()
{ /*新线程执行 */
tid = pthread_self();
if (tid > 0) { //判断当前线程是否成功
gtk_main();
} return 0;
}
void close_window(GtkObject * object, gpointer user_data)
{
gtk_main_quit();
}
void on_window_destroy(GtkObject * object, gpointer user_data)
{
retvalue=1;
gtk_main_quit();
}
void receive_msg(char buf[MAXLINE], GtkWidget *textview2)
{
GtkTextBuffer *buffer;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview2));
GtkTextIter iter;
gtk_text_buffer_get_end_iter(buffer, &iter);
gtk_text_buffer_insert(buffer, &iter, buf, -1);
memset(buf, 0, MAXLINE); //清空buf里面的内容
return;
}
void increase(GtkWidget * widget, GtkLabel * label)
{
char msg[MAXLINE];
char *text;
GtkTextIter start, end;
GtkTextBuffer *buffer;
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(label));
gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buffer), &start, &end); /*获得缓冲区开始和结束位置的Iter */
const GtkTextIter s = start, e = end;
text = (char *) gtk_text_buffer_get_text(GTK_TEXT_BUFFER(buffer), &s, &e, FALSE); /*获得文本框缓冲区文本 */
sprintf(msg, "%s", text);
Write(sockfd, msg, strlen(msg)); //把buf中的数据写给服务器;
sprintf(msg, "我说:%s\n\n", text);
receive_msg(msg, textview2);
gtk_text_buffer_set_text(buffer, "", 0);//清空发送窗口的内容
memset(msg, 0, MAXLINE);
return;
}
int main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
GtkWidget *textview1;
GtkWidget *send;
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "meng.glade", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
send = GTK_WIDGET(gtk_builder_get_object(builder, "send"));
textview1 =GTK_WIDGET(gtk_builder_get_object(builder, "textview1"));
textview2 =GTK_WIDGET(gtk_builder_get_object(builder, "textview2"));
g_signal_connect(send, "clicked", G_CALLBACK(increase), textview1);
g_signal_connect(window, "destroy", G_CALLBACK(on_window_destroy), NULL);
gtk_builder_connect_signals (builder, NULL);
gtk_widget_show_all(window);
//gtk_main ();
struct sockaddr_in servaddr;
sockfd = Socket(AF_INET, SOCK_STREAM, 0);
/*int s;
struct sockaddr_in servaddr,clientaddr;
s=socket(AF_INET,SOCK_DGRAM,0);
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=hton1(INADDR_ANY); //任意本地地址
servaddr.sin_port = htons(SERV_PORT);
*/
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, "192.168.1.106", &servaddr.sin_addr);
servaddr.sin_port = htons(SERV_PORT);
Connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
fcntl(sockfd, F_SETFL, O_NDELAY);
void *sockfe = (void *) sockfd; //类型转换
err = pthread_create(&ntid, NULL, thr_fn, sockfe); /*创建线程 */
if (err != 0) {
fprintf(stderr, "can't create thread:%s\n", strerror(err));
exit(1);
} //失败退出
while (1) {
n = Read(sockfd, buf, MAXLINE); //向服务器接收数据
if(retvalue==1)
{
Close(sockfd);
return 0;
}
if (n > 0)
receive_msg(buf, textview2);//调用接收函数
}
Close(sockfd);
return 0;
}
- 1
- 2
前往页