c网络编程实例(C网络编程)
在windows下,如何用纯C语言实现socket网络编程?
mfc只是对socket进行了一些封装,大部分人做网络编程都是用的原始的socket,比如如下接口都可以在c下进行调用
1.socket()
2.bind()
3.connect()
4.listen()
5.accept()
6.send() 和recv()
7.sendto() 和recvfrom()
8.close() 和shutdown()
9.getpeername()
10.gethostname()
这些接口是在Winsock2.h中定义的不是在mfc中定义的,你只需要包含Winsock2.h头文件和Ws2_32.lib库就可以了。
tcp网络编程(C语言)
以下是两个文件.client.c和server.c
server.c
client.c
运行效果如下
基于c语言,网络编程,服务器接收客户端1的信息发送给客户端2
char
buff2[1024*10];
int
receiveLen;
/*调用accept函数,等待客户端的连接*/
client_fd=accept(sockfd,(struct
sockaddr
*)client_sockaddr,sin_size
/*调用recv函数接收客户端的请求*/
recvbytes=recv(client_fd,buf,BUFFER_SIZE,0)
typedef
int
(WINAPI
ICEPUB_TCPSENDANDRECEIVETEXT)(char
*sendBuff,
int
sendBuffLen,char
*recvBuff,int
recvMaxLen,char*
serverIP,int
serverPort);
ICEPUB_TCPSENDANDRECEIVETEXT
*icePub_tcpSendAndReceiveText
=
0;
HINSTANCE
hDLLDrv
=
LoadLibrary
("icePubDll.dll");
if(hDLLDrv)
{
icePub_tcpSendAndReceiveText=(ICEPUB_TCPSENDANDRECEIVETEXT
*)
GetProcAddress
(hDLLDrv,"icePub_tcpSendAndReceiveText");
}
if(icePub_tcpSendAndReceiveText)
receiveLen=icePub_tcpSendAndReceiveText(buf,recvbytes),buff2,1024*10,"192.168.1.2",8000);
if(hDLLDrv)
FreeLibrary(hDLLDrv);
AfxMessageBox
(buff2);