C语言实的串行通信接口程序
>if (!make-skt(hWnd))/*建立套接字*/
return(FALSE);
if (!set-select(hWnd,FD-CONNECT))/*设置异步连接*/
return(FALSE);
if (!connect-skt(hWnd))/*建立连接*/
return(FALSE);
return(TRUE);
}
BOOL receive-pkt(HWND hWnd)/*接收数据子程序*/
{
HDC dc;
intlength;
int11,12,13;
charlinel,line2,line3;
count ++;/*循环计数器加1*/
if ((length=recv(s,lpBuffer,1024,0))==SOCKET-ERROR)
return(FALSE); /*如果接收数据出错,则返回FALSE*/
if (length==0) /*接收数据长度为零,表示连接中断*/
return(FALSE);
if (dc=GetDC(hWnd)) { /*接收数据成功,显示信息*/
11=wsprintf((LPSTR) line1,"TCP Echo Client No.%d",count);
12=wsprintf((LPSTR) line2,"Received %d bytes", length);
13=wsprintf((LPSTR) line3,"Those are:%c,%c,%c,%c,%c,%c",
buffer,buffer,buffer,buffer,buffer,buffer
);
TextOut(dc, 10, 2, (LPSTR) linel, 11);
TextOut(dc, 10, 22, (LPSTR) line2, 12);
TextOut(dc, 10, 42, (LPSTR) line3, 13);
ReleaseDC(hWnd, dc);
}
return(TRUE);
}
BOOL set-select(HWND hWnd, long lEvent)/*异步选择子程序*/
{
if (WSAAsyncSelect(s,hWnd, UM-SOCK, lEvent)==SOCKET-ERROR) {
AlertUser(hWnd, "WSAAsyncSelect Failed");
return (FALSE);
}
return (TRUE);
}
BOOL make-skt(HWND hWnd)/*建立套接字子程序*/
{
if ((s=socket(AF-INET,sock-type,0))==INVALID-SOCKET) {
AlertUser(hWnd, "Socket Failed");
return (FALSE);
}
return (TRUE);
}
BOOL connect-skt(HWND hWnd)/*建立连接子程序*/
{
memset((void*) &dst-addr, sizeof(dst-addr),0);
dst-addr.sin-family=AF-INET;
dst-addr.sin-port=htons(USERPORT);
dst-addr.sin-addr.s-addr=*((unsigned long *)hostnm.h-addr-list );
if (connect(s, (stru
sizeof(dst-addr))==SOCKET-ERROR) {
AlertUser(hWnd, "Connect Failed");
return (FALSE);
}
return (TRUE);
}
BOOL send-pkt(HWND hWnd, int len)/*发送数据子程序*/
{
int length;
if ((length=send(s,lpBuffer,len,0))==SOCKET-ERROR)
return (FALSE);
else if (length !=len) {
AlertUser(hWnd, "Send Length NOT Match!");
return(FALSE);
}
return (TRUE);
}
我们用最简单的语句编制一个UNIX下基于BSD SOCKET的服务器程序,它在建立连接后,只负责将收到的数据发回去,在连接断开后,服务器关闭套接字返回。要编制在Windows下的服务器程序,可参照客户机程序,使用WINSOCK的异步选择机制。
程序5:UNIX下服务器程序源代码
/*TCP/IP必要的头文件*/
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define USERPORT 3333/*用户定义端口号,与客户机相同*/
#define HOST-IP-ADDR "166.111.8.80"/*我们的主机地址*/
main(int argc, char **argv)
{
char buf;/*buffer for 《C语言实的串行通信接口程序(第4页)》
本文链接地址:http://www.oyaya.net/fanwen/view/171929.html
return(FALSE);
if (!set-select(hWnd,FD-CONNECT))/*设置异步连接*/
return(FALSE);
if (!connect-skt(hWnd))/*建立连接*/
return(FALSE);
return(TRUE);
}
BOOL receive-pkt(HWND hWnd)/*接收数据子程序*/
{
HDC dc;
intlength;
int11,12,13;
charlinel,line2,line3;
count ++;/*循环计数器加1*/
if ((length=recv(s,lpBuffer,1024,0))==SOCKET-ERROR)
return(FALSE); /*如果接收数据出错,则返回FALSE*/
if (length==0) /*接收数据长度为零,表示连接中断*/
return(FALSE);
if (dc=GetDC(hWnd)) { /*接收数据成功,显示信息*/
11=wsprintf((LPSTR) line1,"TCP Echo Client No.%d",count);
12=wsprintf((LPSTR) line2,"Received %d bytes", length);
13=wsprintf((LPSTR) line3,"Those are:%c,%c,%c,%c,%c,%c",
buffer,buffer,buffer,buffer,buffer,buffer
);
TextOut(dc, 10, 2, (LPSTR) linel, 11);
TextOut(dc, 10, 22, (LPSTR) line2, 12);
TextOut(dc, 10, 42, (LPSTR) line3, 13);
ReleaseDC(hWnd, dc);
}
return(TRUE);
}
BOOL set-select(HWND hWnd, long lEvent)/*异步选择子程序*/
{
if (WSAAsyncSelect(s,hWnd, UM-SOCK, lEvent)==SOCKET-ERROR) {
AlertUser(hWnd, "WSAAsyncSelect Failed");
return (FALSE);
}
return (TRUE);
}
BOOL make-skt(HWND hWnd)/*建立套接字子程序*/
{
if ((s=socket(AF-INET,sock-type,0))==INVALID-SOCKET) {
AlertUser(hWnd, "Socket Failed");
return (FALSE);
}
return (TRUE);
}
BOOL connect-skt(HWND hWnd)/*建立连接子程序*/
{
memset((void*) &dst-addr, sizeof(dst-addr),0);
dst-addr.sin-family=AF-INET;
dst-addr.sin-port=htons(USERPORT);
dst-addr.sin-addr.s-addr=*((unsigned long *)hostnm.h-addr-list );
if (connect(s, (stru
ct sockaddr *) & dst-addr,
sizeof(dst-addr))==SOCKET-ERROR) {
AlertUser(hWnd, "Connect Failed");
return (FALSE);
}
return (TRUE);
}
BOOL send-pkt(HWND hWnd, int len)/*发送数据子程序*/
{
int length;
if ((length=send(s,lpBuffer,len,0))==SOCKET-ERROR)
return (FALSE);
else if (length !=len) {
AlertUser(hWnd, "Send Length NOT Match!");
return(FALSE);
}
return (TRUE);
}
我们用最简单的语句编制一个UNIX下基于BSD SOCKET的服务器程序,它在建立连接后,只负责将收到的数据发回去,在连接断开后,服务器关闭套接字返回。要编制在Windows下的服务器程序,可参照客户机程序,使用WINSOCK的异步选择机制。
程序5:UNIX下服务器程序源代码
/*TCP/IP必要的头文件*/
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define USERPORT 3333/*用户定义端口号,与客户机相同*/
#define HOST-IP-ADDR "166.111.8.80"/*我们的主机地址*/
main(int argc, char **argv)
{
char buf;/*buffer for 《C语言实的串行通信接口程序(第4页)》