#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 5555
#define BACKLOG 10
#define MAX(a,b) ((a)>(b)?(a):(b))
void str_echo(int)
void server()
{
int sockfd,client_fd
struct sockaddr_in my_addr
struct sockaddr_in remote_addr
socklen_t sin_size
if((sockfd=socket(AF_INET,SOCK_STREAM,0))== -1)
{
perror("socket create error!")
exit(1)
}
my_addr.sin_family=AF_INET
my_addr.sin_port=htons(SERVPORT)
my_addr.sin_addr.s_addr=INADDR_ANY
bzero(&(my_addr.sin_zero),8)
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
perror("bind error!")
exit(1)
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen error!")
exit(1)
}
while(1)
{
sin_size=sizeof(struct sockaddr_in)
if((client_fd=accept(sockfd,(struct sockaddr *)&remote_addr,&sin_size))==-1)
{
perror("accept error!")
continue
}
printf("received a connection from %s\n",inet_ntoa(remote_addr.sin_addr))
if(!fork())
{
close(sockfd)
str_echo(client_fd)
exit(0)
}
close(client_fd)
}
}
void str_echo(int sockfd)
{
int maxfd
char sendbuf[1024]={0}
char recvbuf[1024]={0}
fd_set rfds
while(1)
{
FD_SET(0,&rfds)
FD_SET(sockfd,&rfds)
maxfd=MAX(0,sockfd)+1
select(maxfd,&rfds,NULL,NULL,NULL)
if(FD_ISSET(sockfd,&rfds))
{
bzero(recvbuf,1024)
recv(sockfd,recvbuf,1024,0)
printf("C:%s\n",recvbuf)
}
if(FD_ISSET(0,&rfds))
{
read(0,sendbuf,1024)
send(sockfd,sendbuf,strlen(sendbuf),0)
bzero(sendbuf,1024)
}
}
}
int main()
{
server()
return 0
}
/*客户端*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 5555
#define MAXSIZE 1024
#define MAX(a,b) ((a)>(b)?(a):(b))
void client(int argc,char **argv)
{
int sockfd,maxfd
char sendbuf[MAXSIZE]={0}
char recvbuf[MAXSIZE]={0}
// struct hostent *host
struct sockaddr_in serv_addr
fd_set rfds
/* if(argc<2)
{
fprintf(stderr,"Please enter the sserver's name!\n")
exit(1)
}
if((host=gethostbyname(argv[1]))==NULL)
{
herror("get host by name error!\n")
exit(1)
}
*/
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket creat error!\n")
exit(1)
}
serv_addr.sin_family=AF_INET
serv_addr.sin_port=htons(SERVPORT)
// serv_addr.sin_addr=*((struct in_addr *)host->h_addr)
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1")
bzero(&(serv_addr.sin_zero),8)
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
{
perror("connection error!\n")
exit(1)
}
FD_ZERO(&rfds)
while(1)
{
FD_SET(0,&rfds)
FD_SET(sockfd,&rfds)
maxfd=MAX(0,sockfd)+1
select(maxfd,&rfds,NULL,NULL,NULL)
if(FD_ISSET(sockfd,&rfds))
{
bzero(recvbuf,MAXSIZE)
recv(sockfd,recvbuf,MAXSIZE,0)
printf("S:%s\n",recvbuf)
}
if(FD_ISSET(0,&rfds))
{
fgets(sendbuf,MAXSIZE,stdin)
send(sockfd,sendbuf,strlen(sendbuf),0)
bzero(sendbuf,MAXSIZE)
}
}
// close(sockfd)
}
int main(int argc,char **argv)
{
client(argc,argv)
return 0
}
GTK是C++类库吧,怎么用C语言来写界面,你老师脑残了啊你可以去问候他老母或者自己找下GTK的资料,socket是关键,多线程和并发什么的,不要偷懒,不然出来工作就跟个傻瓜一样了
用指针数组啦 例如 :GtkWidget *btn[10]
一下例子为:3x3 按钮
#include <gtk/gtk.h>int main(int argc, char *argv[])
{
GtkWidget *window
GtkWidget *table
GtkWidget *button[9]
int i
int j
gtk_init(&argc, &argv)
window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
g_signal_connect(G_OBJECT(window), "delete_event",
G_CALLBACK(gtk_main_quit), NULL)
gtk_window_set_title(GTK_WINDOW(window), "控件数组")
gtk_window_set_default_size(GTK_WINDOW(window), 500, 500)
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER)
gtk_container_set_border_width(GTK_CONTAINER(window), 40)
table = gtk_table_new(3, 3, FALSE)
gtk_container_add(GTK_CONTAINER(window), table)
for(i=0i<3i++)
{
for(j=0j<3j++)
{
char buf[256] = ""
sprintf(buf, "按钮%d\0", i*3+j+1)
button[i*3+j] = gtk_button_new_with_label(buf)
gtk_table_attach_defaults(GTK_TABLE(table), button[i*3+j], j, j+1, i,i+1)
}
}
gtk_widget_show_all(window)
gtk_main()
return FALSE
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)