資源簡介
飛鴿傳書(IP Messenger,簡為IPMsg)是一個小巧方便的即時通信軟件,它適合用于局域網內甚至廣域網間進行實時通信和文檔共享。特別是在局域網內傳送文件/文件夾的速度非常快! 代碼有注釋,適合初學者

代碼片段和文件信息
/*?************************************************************************
?*???????Filename:??command.c
?*????Description:??
?*????????Version:??1.0
?*????????Created:??2010年10月10日?09時23分07秒
?*???????Revision:??none
?*???????Compiler:??gcc
?*?????????Author:??YOUR?NAME?()?
?*????????Company:??
?*?************************************************************************/
?
?
#include?“myinclude.h“
#include?“user_manager.h“
#include?“communication.h“
#include?“user_interface.h“
static?int?udpfd;??//UDP?socket描述符
static?char?user_name[20]?=?““;//用戶名
static?char?host_name[30]?=?““;//主機名
FINFO?recv_buf[5];//保存一次接收的n個文件的信息,每個元素
??????????????????//保存一共文件的信息,方便以后索引
??
/*創建一個套接字,將其與本機的ip和
2424端口綁定并設置其多播的屬性*/ ??
void?create_server()
{
int?broadcast=1;
struct?sockaddr_in?addr?=?{AF_INET};??//初始化一個網絡地址
addr.sin_port?=?htons(PORT);
addr.sin_addr.s_addr?=?htonl(INADDR_ANY);
udpfd?=?socket(AF_INETSOCK_DGRAM0);
if(udpfd?0)
IPMSG_OUT_MSG_COLOR(perror(“Socket?UDP“);)
if(bind(udpfd?(struct?sockaddr*)&addr?sizeof(addr))<0)
IPMSG_OUT_MSG_COLOR(perror(“Bind?UDP“);)
setsockopt(udpfdSOL_SOCKETSO_BROADCAST&broadcast?sizeof(int));
}
/*下面幾個函數都是讓其他文件可以使用這些內
部連接的變量,在其他文件對這些變量賦值*/
int?udp_fd(void)
{
return?udpfd;
}
char?*user(void)
{
return?user_name;
}
char?*host(void)
{
return?host_name;
}
/*該函數,廣播一個IPMSG_BR_ENTRY的包文,
可以讓同一個局域網中端口為2425的應用程
序收到這個消息,以告訴對方,該用戶已經
上線*/
void?online(void)
{
char?buf[100]=““;
struct?sockaddr_in?addr?=?{AF_INET};
int?t?=?time((time_t?*)NULL);
//初始化目標網絡地址
addr.sin_port?=?htons(PORT);?
addr.sin_addr.s_addr?=?inet_addr(“192.168.220.255“);
//學習這種組建buf的方式
int?len?=?sprintf(buf“1:%d:%s:%s:%ld:%s“?
??tuser_namehost_nameIPMSG_BR_ENTRYuser_name);
//廣播一個包文
sendto(udpfd?buf?len?0?(struct?sockaddr*)&addrsizeof(addr));
}
/*系統初始化函數*/
void?sysinit(char?*user?char?*host)
{
//只要使用內部連接變量的函數與該變量在同一個文件中定義,
//則無論該函數在哪里出現都不會因為引用該變量出錯
strcpy(user_nameuser);
strcpy(host_namehost);
create_server();
online();
}
/*應答上線用戶*/
static?void?answer_entry(int?pkgnum?struct?sockaddr_in?addr)
{
char?buf[100];
int?len?=?0;
//組建包文
len?=?sprintf(buf“1:%d:%s:%s:%ld:%s“pkgnumuser()
??host()IPMSG_ANSENTRYuser());
//發送包文 ??
sendto(udp_fd()buflen0(struct?sockaddr*)&addrsizeof(addr));
}
/*打印收到的消息,發送該消息的包文,不帶文件屬性信息*/
static?void?print_msg(char?*sender?char?*msgbuf)
{
printf(“\n%s:?%s\n\n“?sender?msgbuf);//打印發送者,消息
write(1“\rIPMSG:“7);
fflush(stdout);
}
/*判斷接收到包文是否是接收文件的包文*/
static?int?isrecvfile(long?cmddata)
{
//cmddata為接收到的命令字
if((IPMSG_SENDMSG|IPMSG_FILEATTACHOPT|IPMSG_READCHECKOPT|
IPMSG_SENDCHECKOPT|IPMSG_SECRETOPT)?==?cmddata)
return?1;
else
return?0;
}
/*保存等待接收的文件信息(報文的包編號、文件序
號、文件名、文件大小、最新修改時間、偏移量、文
件模式、發送者的ip)*/
static?void?save_file_info(int?msgnumlong?cmddatachar?*msgbufchar?*sender
char?*attrbufstruct?sockaddr_in?addr)
{
int?i?=?0;
int?j?=?0;
int?t?=?time((time_t?*)NULL);?//獲取包編號
char?*p[5]?=?{NULL};??????????//用于存放文件的屬性,一共可以存放
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????16384??2018-05-23?15:29??ipmsg\.communication.c.swp
?????文件???????6802??2010-10-13?00:26??ipmsg\communication.c
?????文件????????887??2010-10-12?16:21??ipmsg\communication.h
?????文件???????4928??2010-10-12?14:30??ipmsg\communication.o
?????文件????????126??2010-10-12?11:14??ipmsg\down\hello.txt
?????文件????????397??2010-10-12?13:25??ipmsg\down\log.c
?????文件???????3772??2010-10-12?14:35??ipmsg\down\main.c
?????文件???????2055??2010-10-12?14:31??ipmsg\down\txt.c
?????文件??????19592??2010-10-12?16:22??ipmsg\ipmsg
?????文件???????5942??2010-10-10?14:31??ipmsg\ipmsg.h
?????文件????????965??2010-10-12?16:14??ipmsg\main.c
?????文件???????1232??2010-10-12?16:22??ipmsg\main.o
?????文件????????195??2010-10-11?08:10??ipmsg\makefile
?????文件????????804??2010-10-11?07:58??ipmsg\myinclude.h
?????文件???????9043??2018-05-10?15:52??ipmsg\myinclude.h.gch
?????文件???????5124??2010-10-12?16:33??ipmsg\mytcp.c
?????文件????????598??2010-10-12?16:16??ipmsg\mytcp.h
?????文件???????6492??2010-10-12?16:22??ipmsg\mytcp.o
?????文件???????8348??2010-10-12?15:18??ipmsg\user_interface.c
?????文件????????897??2010-10-12?16:19??ipmsg\user_interface.h
?????文件???????5716??2010-10-12?15:18??ipmsg\user_interface.o
?????文件???????3473??2010-10-12?16:22??ipmsg\user_manager.c
?????文件???????1023??2010-10-12?16:22??ipmsg\user_manager.h
?????文件???????2360??2010-10-12?16:22??ipmsg\user_manager.o
?????目錄??????????0??2010-11-26?13:05??ipmsg\down
?????目錄??????????0??2018-05-23?15:42??ipmsg
-----------?---------??----------?-----??----
???????????????107155????????????????????26
評論
共有 條評論