原本以为c是跨平台,所以,c在windows下和linux下的程序应该是类似于Java,什么都不用改变的,今儿才恍然大悟,他们的类库不一样啊……
下面我贴出来一个windows下的c语言socket通信例子,这里我们客户端传递一个字符串,服务器端进行接收。
【实际上我们需要完成的二进制流的传输,需要使用unsignedchar来实现,因为c里没有byte数据类型,这里我们不以byte为例,因为效果不会很直观,我们采取最简单的字符串交互。】
【服务器端】
#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#include <winsock2.h>
#define SERVER_PORT 5208 //侦听端口
void main()
{
WORDwVersionRequested;
WSADATAwsaData;
int ret,nLeft, length;
SOCKETsListen, sServer; //侦听套接字,连接套接字
struct sockaddr_in saServer, saClient; //地址信息
char*ptr;//用于遍历信息的指针
//WinSock初始化
wVersionRequested=MAKEWORD(2, 2); //希望使用的WinSock DLL 的版本
ret=WSAStartup(wVersionRequested, &wsaData);
if(ret!=0)
{
printf("WSAStartup() failed!n");
return;
}
//创建Socket,使用TCP协议
sListen=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sListen== INVALID_SOCKET)
{
WSACleanup();
printf("socket() faild!n");
return;
}
//构建本地地址信息
saServer.sin_family = AF_INET; //地址家族
saServer.sin_port = htons(SERVER_PORT); //注意转化为网络字节序
saServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //使用INADDR_ANY 指示任意地址
//绑定
ret = bind(sListen, (struct sockaddr *)&saServer,sizeof(saServer));
if (ret ==SOCKET_ERROR)
{
printf("bind() faild! code:%dn", WSAGetLastError());
closesocket(sListen); //关闭套接字
WSACleanup();
return;
}
//侦听连接请求
ret =listen(sListen, 5);
if (ret ==SOCKET_ERROR)
{
printf("listen() faild! code:%dn", WSAGetLastError());
closesocket(sListen); //关闭套接字
return;
}
printf("Waiting for client connecting!n");
printf("Tips: Ctrl+c to quit!n");
//阻塞等待接受客户端连接
while(1)//循环监听客户端,永远不停止,所以,在本项目中,我们没有心跳包。
{
length =sizeof(saClient);
sServer = accept(sListen,(struct sockaddr *)&saClient,&length);
if (sServer ==INVALID_SOCKET)
{
printf("accept()faild! code:%dn", WSAGetLastError());
closesocket(sListen);//关闭套接字
WSACleanup();
return;
}
charreceiveMessage[5000];
nLeft =sizeof(receiveMessage);
ptr = (char*)&receiveMessage;
while(nLeft>0)
{
//接收数据
ret = recv(sServer,ptr, 5000,0);
if(ret == SOCKET_ERROR)
{
printf("recv()failed!n");
return;
}
if (ret == 0)//客户端已经关闭连接
{
printf("Clienthas closed the connectionn");
break;
}
nLeft -=ret;
ptr +=ret;
}
printf("receive message:%sn", receiveMessage);//打印我们接收到的消息。
}
// closesocket(sListen);
// closesocket(sServer);
// WSACleanup();
}
【客户端】
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#define SERVER_PORT 5208 //侦听端口
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int ret;
SOCKET sClient; //连接套接字
struct sockaddr_in saServer; //地址信息
char *ptr;
BOOL fSuccess = TRUE;
//WinSock初始化
wVersionRequested = MAKEWORD(2, 2); //希望使用的WinSock DLL的版本
ret =WSAStartup(wVersionRequested, &wsaData);
if(ret!=0)
{
printf("WSAStartup() failed!n");
return;
}
//确认WinSock DLL支持版本2.2
if(LOBYTE(wsaData.wVersion)!=2|| HIBYTE(wsaData.wVersion)!=2)
{
WSACleanup();
printf("Invalid WinSock version!n");
return;
}
//创建Socket,使用TCP协议
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient ==INVALID_SOCKET)
{
WSACleanup();
printf("socket() failed!n");
return;
}
//构建服务器地址信息
saServer.sin_family = AF_INET;//地址家族
saServer.sin_port =htons(SERVER_PORT); //注意转化为网络节序
saServer.sin_addr.S_un.S_addr= inet_addr("192.168.1.127");
//连接服务器
ret = connect(sClient, (struct sockaddr*)&saServer, sizeof(saServer));
if (ret == SOCKET_ERROR)
{
printf("connect() failed!n");
closesocket(sClient); //关闭套接字
WSACleanup();
return;
}
char sendMessage[]="hello thisis client message!";
ret = send (sClient, (char*)&sendMessage, sizeof(sendMessage),0);
if (ret ==SOCKET_ERROR)
{
printf("send() failed!n");
}
else
printf("client info has been sent!");
closesocket(sClient);//关闭套接字
WSACleanup();
}