|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
最近在写通信程序,给大家分享点简单的小东西.是用来实现通信的,
一个Server端 一个Client端~实现的就是Client可以给Server发消息,
对网络编程有兴趣的朋友可以学习一下,当然这只是最基础的..
学完c或者c++可以看一看,大神就略过好了...
都是基于Socket来实现,然后模拟HTTP协议,或者局域网通信什么的都可以基于这个实现.
需要注意的是Server和Client分开来编译,先运行Server,再运行Client,
运行起来之后也许会有网络的系统提示跳出来.允许了就好,当然要是不放心,你可以断了网来弄,,默认的都是你本机的IP.
暂时只能实现Client给Server发消息,没有弄线程什么的.要是想实现QQ那样的得在往上加工很多东西~~这只是个简单的分享~//Server//
#include<stdio.h>
#include<winsock2.h> //Windows Socket 头文件
#include<string.h>
#pragma comment(lib,"WS2_32.lib") //Windows Socket所需要添加的动态库
//全局变量//
WORD Version; //确定winsock的版本
WSADATA WSAdata; //信息返回的变量
SOCKET ServSocket; //socket 名称
SOCKADDR_IN Server;
SOCKET ClientSocket; //socket 名称
SOCKADDR_IN Client;
//函数定义//
int INIT();
int InitSocket();
int BIND();
int LISTEN();
int Accept();
int RECV();
void Exit();
int InitSocket() //创建套接字.
{
DWORD error ;
Version=MAKEWORD(2,2); //winsock版本2.2
if(WSAStartup(Version,&WSAdata)!=ERROR_SUCCESS) //请求winsock2.2版
{
error = GetLastError();
printf("Windows Socket is not responded\n%d\n ",error); //错误返回.
system("pause");
exit(0);
}
ServSocket=INVALID_SOCKET;
ServSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(ServSocket==INVALID_SOCKET)
{
error = GetLastError();
printf("Windows Socket can't be open\n%d\n",error); //错误返回.
system("pause");
exit(0);
}
printf("Socket Is Ready\n");
return 0;
}
int BIND()
{
int ret=-1;
DWORD error ;
Server.sin_addr.s_addr =INADDR_ANY;
Server.sin_family=AF_INET;
Server.sin_port=htons(2222);
ret=bind(ServSocket,(SOCKADDR*)&Server,sizeof(Server));
if(ret==SOCKET_ERROR)
{
error = GetLastError();
printf("Bind error \n%d\n",error);
system("pause");
exit(0);
}
printf("Bind seccess\n");
return 0;
}
int LISTEN()
{
DWORD error;
int ret=listen(ServSocket,2);
if(ret==SOCKET_ERROR)
{
error = GetLastError();
printf(" listen error %d\n",error);
system("pause");
exit(0);
}
printf("Listen seccess\n");
return 0;
}
int Accept()
{
int x=sizeof(Client);
ClientSocket=INVALID_SOCKET;
ClientSocket=accept(ServSocket,(SOCKADDR *)&Client,&x);
if(ClientSocket==INVALID_SOCKET)
{
printf("Accept error\n");
}
return 0;
}
int RECV()
{
int x=0;
char str[1024];
while(1)
{
memset(str,0,sizeof(str));
x=recv(ClientSocket,str,sizeof(str),0);
if(x>=0)
{
printf("%s\n",str);
}
else
{
printf("客户端关闭");
Exit();
}
Sleep(200);
}
return 0;
}
int INIT()
{
InitSocket();
BIND();
LISTEN();
Accept();
return 0;
}
void Exit()
{
closesocket(ServSocket); //关闭socket.
closesocket(ClientSocket);
WSACleanup(); //进行所有的清除工作.
exit(0);
}
int main(void)
{
INIT();
RECV();
return 0;
}
//Client//
#include<stdio.h>
#include<winsock2.h> //Windows Socket 头文件
#include<string.h>
#pragma comment(lib,"WS2_32.lib") //Windows Socket所需要添加的动态库
//全局变量//
WORD Version; //确定winsock的版本
WSADATA WSAdata; //信息返回的变量
SOCKET ClientSocket; //socket 名称
SOCKADDR_IN Server;
HANDLE hSend;
HANDLE hRecv;
//函数定义//
int InitSocket();
int Connect();
void SEND();
void Exit();
int InitSocket() //创建套接字.
{
Version=MAKEWORD(2,2); //winsock版本2.2
if(WSAStartup(Version,&WSAdata)!=ERROR_SUCCESS) //请求winsock2.2版
{
printf("Windows Socket is not responded\n"); //错误返回.
exit(0);
}
ClientSocket=INVALID_SOCKET; //设置默认为空
ClientSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(ClientSocket==INVALID_SOCKET)
{
printf("Windows Socket can't be open\n"); //错误返回.
exit(0);
}
return 0;
}
int Connect() //连接
{
int ret=1;
DWORD error;
Server.sin_addr.s_addr = inet_addr("127.0.0.1"); //本机IP
Server.sin_family=AF_INET; //使用的协议TCP
Server.sin_port=htons(2222); //端口
ret=connect(ClientSocket,(SOCKADDR*)&Server,sizeof(SOCKADDR));
if(ret<0)
{
error = GetLastError();
printf("connect error. err.num:%d\n",error);
exit(0);
}
printf("connect success\n");
return 0;
}
void SEND()
{
char str[1024]={0};
while(1)
{
memset(str,0,sizeof(str));
gets(str);
send(ClientSocket,str,sizeof(str),0);
}
}
void Exit()
{
closesocket(ClientSocket); //关闭socket.
WSACleanup();
exit(0);//进行所有的清除工作.
}
int main()
{
InitSocket();
Connect() ;
SEND();
return 0;
}
|
|