资源简介
掌握C语言形式的Socket编程接又用法,能够正确发送和接收网络数据包; 分为客户端和服务端
![](http://www.nz998.com/pic/56989.jpg)
代码片段和文件信息
/*socket tcp客户端*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include “public.h“
#define BUFSIZE 1024
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct RequestPkt
{
char type[5]; //time name list msge quit
char number[3]; //the order of the client would recieve the msg
char content[800]; //the content of the target client woulf receive
};
struct MsgPkt
{
char msg; //$1 / 0
char order[3]; //order of the client who send the msg
char IP[20]; ///if it is responsePkt IP == type; port == number; content == content
char port[5];
char content[800];
};
void PktRequst(struct RequestPkt* t char* s);
void PktMsg(struct MsgPkt* t char* s);
void cut(char* s char* tar int order)
{
int i = 0;
int j = 0;
int k = 0;
while(i != order)
{
if(tar[j++] == ‘$‘) i++;
}
while(tar[j] != ‘$‘)
{
s[k++] = tar[j++];
}
s[k] = ‘\0‘;
}
void PktRequst(struct RequestPkt* t char* s)
{
strcpy(s “\0“);
strcpy(s “$“);
strcat(s t->type);
if(strcmp(t->type “msge“) == 0)
{
strcat(s “$“);
strcat(s t->number);
strcat(s “$“);
strcat(s t->content);
}
strcat(s “$“);
}
void unPktReq(char* s struct RequestPkt* t)
{
cut(t->type s 1);
if(s[1] == ‘m‘)
{
cut(t->number s 2);
cut(t->content s 3);
}
}
void PktMsg(struct MsgPkt* t char* s)
{
strcpy(s “\0“);
strcpy(s “$“);
s[1] = t->msg;
strcat(s “$“);
if(t->msg == ‘1‘ || t->msg == ‘3‘)
{
strcat(s t->order);
strcat(s “$“);
strcat(s t->IP);
strcat(s “$“);
strcat(s t->port);
strcat(s “$“);
strcat(s t->content);
strcat(s “$“);
} else
{
strcat(s “$“);
strcat(s t->IP);
strcat(s “$“);
if(strcmp(t->IP “msge“) == 0)
{
strcat(s t->port);
strcat(s “$“);
strcat(s “$“);
}
if(strcmp(t->IP “list“) == 0)
{
strcat(s t->port);
strcat(s “$“);
strcat(s t->content);
strcat(s “$“);
}
if(strcmp(t->IP “name“) == 0 || strcmp(t->IP “time“) == 0)
{
strcat(s “$“);
strcat(s t->content);
strcat(s “$“);
}
}
}
int unPktMsg(char* s struct MsgPkt* t)
{
// if(s[1] != ‘1‘) return 0;
t->msg = s[1];
cut(t->order s 2);
cut(t->port s 4);
cut(t->IP s 3);
cut(t->content s 5);
return 1;
}
/////////////////////////////////////////////////////
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-04-11 14:27 code\
文件 9960 2018-04-11 14:27 code\c.c
目录 0 2018-05-14 08:11 __MACOSX\
目录 0 2018-05-14 08:11 __MACOSX\code\
文件 2277 2018-04-11 14:27 __MACOSX\code\._c.c
文件 14344 2018-04-08 17:23 code\client
文件 10578 2018-04-11 14:27 code\s.c
文件 3445 2018-04-11 14:27 __MACOSX\code\._s.c
文件 14300 2018-04-08 17:21 code\server
- 上一篇:个人银行账户管理程序C++
- 下一篇:实训项目-基于MFC网络监控源代码可直接运行
相关资源
- C++中头文件与源文件的作用详解
- 基于mfc的多线程文件传输
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- C语言代码高亮html输出工具
- 猜数字游戏 c语言代码
- C语言课程设计
- 数字电位器C语言程序
- CCS FFT c语言算法
- 使用C语言编写的病房管理系统
- 通信过程中的RS编译码程序(c语言)
- 利用C++哈希表的方法实现电话号码查
- 计算机二级C语言上机填空,改错,编
- 用回溯法解决八皇后问题C语言实现
- 移木块游戏,可以自编自玩,vc6.0编写
- 简易教务管理系统c语言开发文档
- 操作系统课设 读写者问题 c语言实现
- 小波变换算法 c语言版
- C流程图生成器,用C语言代码 生成C语
- 3des加密算法C语言实现
- 简单的C语言点对点聊天程序
- 单片机c语言源程序(51定时器 八个按
- C++纯文字DOS超小RPG游戏
- 个人日常财务管理系统(C语言)
- MFC数字钟(基于VC6.0)
- c语言电子商务系统
- 小甲鱼C语言课件 源代码
- 将图片转换为C语言数组的程序
- 安科瑞智能电能表MODBUS通讯程序 VC6
- C语言实现的一个内存泄漏检测程序
评论
共有 条评论