教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 小学教育 >

网络聊天程序设计(Linux版)(2)

来源:网络收集 时间:2026-02-09
导读: 课设 5. 设计总结 转眼间为期一周的嵌入式系统开发课程设计转眼就结束了,这次课程设计,我学会了许多课本上学不到的东西,同时也加强了我的动手、思考和解决问题的能力以及学会了相互帮助,相互团结的团队精神,受

课设

5. 设计总结

转眼间为期一周的嵌入式系统开发课程设计转眼就结束了,这次课程设计,我学会了许多课本上学不到的东西,同时也加强了我的动手、思考和解决问题的能力以及学会了相互帮助,相互团结的团队精神,受益匪浅。

从前的学习过程过于浮浅,只是流于表面的理解,而现在要做课程设计,就不得不要求我们对所用到的知识有更深层次的理解。因为课程设计的内容比及书本中的理论知识而言,更接近于现实生活,而理论到实践的转化往往是一个艰难的过程,它犹如一只拦路虎,横更在我们的面前。

通过这次课程设计使我懂得了理论与实际相结合是很重要的,只有理论知识是远远不够的,只有把所学的理论知识与实践相结合起来,从理论中得出结论,才能真正为社会服务,从而提高自己的实际动手能力和独立思考的能力。在设计的过程中遇到问题,可以说得是困难重重,这毕竟第一次做的,难免会遇到过各种各样的问题,同时在设计的过程中发现了自己的不足之处,对以前所学过的知识理解得不够深刻,掌握得不够牢固。

感谢谢老师的悉心教导和认真帮助,在我们遇到各种困难时给予我们最深刻的指导,让我们能够较顺利的完成此次课程设计。

本次课程设计有李钱波、林洪、王宗敏集体完成。

参考文献

(1) 华清远见嵌入式培训中心编著 《嵌入式Linux应用程序开发》(第二版)人民邮电出版社 2011年7月

(2) 谢小云编著《嵌入式系统综合项目》江西理工大学应用科学学院出版社,2011年

(3) 王天苗,魏洪兴编著《嵌入式系统设计与实例开发》(第三看版)清华大学出版社,2007年

课设

附录

/******* 客户端程序 client.c ************/

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

#include <string.h>

#include <netdb.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#define TRUE 1

#define PORT 5000

static int sockfd;

void recvfromserver( ) //接受服务器消息线程入口函数

{ char mes[1024];

int nbytes=0;

while( 1 )

{ memset(mes,0,sizeof(mes));

nbytes=read(sockfd,mes,sizeof(mes));

if(nbytes>0)

{ mes[nbytes]='\0';

printf("%s\n",mes);

}

}

pthread_exit(NULL);

}

int main(int argc, char *argv[])

{

int sockfd;

char buffer[1024];

struct sockaddr_in server_addr;

struct hostent *host;

int portnumber,nbytes;

char strhost[16];

char clientname[20];

char mes[1024];

int thr_id; /* thread ID for the newly created thread */ pthread_t p_thread; /* thread's structure */

if(argc!=1)

{ fprintf(stderr,"Usage:%s \a\n",argv[0]);

exit(1);

}

Printf ("请输入服务器ip地址\n");

课设

//

} scanf("%s",strhost); if((host=gethostbyname(strhost))==NULL) { fprintf(stderr,"Gethostname error\n"); exit(1); } /* 客户程序开始建立 sockfd 描述符 */ printf ("正在建立套接口...\n"); if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket Error:%s\a\n",strerror(errno)); exit(1); } /* 客户程序填充服务端的资料 */ bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_port=htons(PORT); server_addr.sin_addr=*((struct in_addr *)host->h_addr); printf ("套接口创建成功,正在链接服务器...\n"); /* 客户程序发起连接请求 */ if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) { fprintf(stderr,"Connect Error:%s\a\n",strerror(errno)); exit(1); } /* 连接成功了 */ Printf ("链接服务器成功\n欢迎来到聊天室\n"); Printf ("请输入你的用户昵称\n"); scanf("%s",clientname); write(sockfd,clientname,sizeof(clientname)); Printf ("\n\n开始聊天吧(\"Quit\"断开连接)\n\n"); thr_id = pthread_create(&p_thread, NULL, recvfromserver, NULL); while(1) { memset(buffer,0,sizeof(buffer)); memset(mes,0,sizeof(mes)); scanf("%s",buffer); strcat(mes,clientname); strcat(mes,":"); strcat(mes,buffer); // printf("main thread %s\n",mes); if((write(sockfd,mes,sizeof(mes)))==-1) { fprintf(stderr,"Write Error:%s\n",strerror(errno)); exit(1); } if(strcmp(buffer,"Quit")==0) { break; } } /* 结束通讯 */ close(sockfd); exit(0);

课设

/******* 服务器程序 (server.c) ************/

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

#include <string.h>

#include <netdb.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <sys/socket.h>

#define MAXLINE 1000 //在一条消息中最大的输出字符数

#define LISTENQ 20 //最大监听队列

#define PORT 5000 //监听端口

#define MAXFD 20 //最大的在线用户数量

void *get_client(void *);

int sockfd,i;

static int maxi=0; //maxi表示当前client数组中最大的用户的i值 static int client[MAXFD];

void recvandsend(void) //监听转发线程入口函数

{ int index=0;

int nbytes=0;

char buffer[1024];

int len;

int outindex=0;

while(1)

{ if(maxi>0)

{ memset(buffer,0,sizeof(buffer));

nbytes=0;

index++;

nbytes=read(client[index++],buffer,sizeof(buffer));

printf("%d,%d\n",index,client[index]);

if(nbytes>0)

{ buffer[nbytes]='\0';

printf(" %s\n",buffer);

outindex=0;

while(outindex<maxi)

if(write(client[outindex++],buffer,sizeof(buffer))=1)

{ fprintf(stderr,"Write Error:%s\n",strerror(errno)); exit(1);

}

}

}

if(index>=maxi)

index=0;

}

…… 此处隐藏:1884字,全部文档内容请下载后查看。喜欢就下载吧 ……
网络聊天程序设计(Linux版)(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/39934.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)