貼上伺服器的**:
// 回顯伺服器3.cpp : 定義控制台應用程式的入口點。
//#include "stdafx.h"
#include #include #include #include #pragma comment(lib,"wsock32.lib")
#define serverprot 6000
#define maxsize 100
//函式宣告
int recvn(socket s, char * recvbuf, unsigned int fixedlen);
int recvvl(socket s, char * recvbuf, unsigned int recvbuflen);
int _tmain(int argc, _tchar* argv)
if(lobyte(wsadata.wversion)!=2||hibyte(wsadata.wversion)!=2)
//建立監聽套接字
socket slisten;
slisten = socket(af_inet,sock_stream,0);
if(slisten == invalid_socket)
//構建伺服器本地位址資訊
struct sockaddr_in serveraddr;
serveraddr.sin_family = af_inet;
serveraddr.sin_port = htons(serverprot);
serveraddr.sin_addr.s_un.s_addr = inaddr_any;
//繫結
ret = bind(slisten,(sockaddr*)&serveraddr,sizeof(serveraddr));
if(ret == socket_error)
//偵聽連線請求
ret = listen(slisten,5);
if(ret == socket_error)
printf("waiting for client connecting...\n");
//接收客戶端連線
struct sockaddr_in clientaddr;
int clientaddrlen = sizeof(clientaddr);
socket sserver;//連線套接字
char buff[maxsize];//接收快取
char sendbuf[maxsize];//傳送快取
unsigned int slen=0; //傳送資料長度
unsigned int buflen=maxsize;
sserver = accept(slisten,(struct sockaddr*)&clientaddr,&clientaddrlen);
if(sserver == invalid_socket)
while(true) //迴圈接收資料
else if(wsaeshutdown == nerrcode)
else if (wsaetimedout == nerrcode)
else if (wsaeconnreset == nerrcode)
else{}
closesocket(sserver);
closesocket(slisten);
wsacleanup();
return -1;
} sprintf(sendbuf,"echo:%s",buff);
printf("%s\n",sendbuf);
slen = (unsigned int)strlen(sendbuf);
slen = htonl(slen);
ret = send(sserver,(char*)&slen,sizeof(unsigned int),0);
if (socket_error == ret)
else if(wsaeshutdown == nerrcode)
else if (wsaetimedout == nerrcode)
else{}
closesocket(sserver);
closesocket(slisten);
wsacleanup();
return -1;
} ret = send(sserver,sendbuf,strlen(sendbuf),0);
if (socket_error == ret)
else if(wsaeshutdown == nerrcode)
else if (wsaetimedout == nerrcode)
else{}
closesocket(sserver);
closesocket(slisten);
wsacleanup();
return -1;
} }closesocket(sserver);
closesocket(slisten);
wsacleanup();
return 0;
}//指定長度接收
int recvn(socket s, char * recvbuf, unsigned int fixedlen)
if ( iresult == 0 )
//printf("接收到的位元組數: %d\n", iresult);
//接收快取指標向後移動
recvbuf +=iresult;
//更新cnt值
cnt -=iresult;
} return fixedlen;
}int recvvl(socket s, char * recvbuf, unsigned int recvbuflen)
else
} //轉換網路位元組順序到主機位元組順序
reclen = ntohl( reclen );
if ( reclen > recvbuflen )
else
}reclen -= recvbuflen;
//處理最後一段資料長度
if ( reclen < recvbuflen )
recvbuflen = reclen;
} printf("可變長度的訊息超出預分配的接收快取\r\n");
return -1;
} //接收可變長訊息
iresult = recvn( s, recvbuf, reclen );
if ( iresult != reclen )
else
} return iresult;
}
回射程式3 客戶端 客戶伺服器不定長接收資料
又是乙個回射程式!畢竟還是乙隻小菜鳥,多練練,多寫寫沒有壞處滴!這次的回射程式思路如下 客戶端和伺服器都使用了新的函式recvvl來接收資料了!這是乙個自己編寫的函式,主要作用是接收不定長的資料。整個程式的設計思路是這樣的 客戶端首先將訊息體的長度傳送給伺服器,再將訊息體傳送給伺服器。在伺服器端先接...
用select實現TCP回射程式(伺服器及客戶端)
該程式系參考 unp 來編寫的,主要用來練習select及shutdown函式的使用。伺服器 include include include include include include include include include include include define port 888...
回射客戶 伺服器模型(1)
最近在學習socket程式設計,根據自己的學習過程及學習筆記,下面來梳理一下如何實現乙個簡單的回射客戶 伺服器模型,也藉此來熟悉一下socket bind listen accept connect這些函式的使用。簡單的回射客戶 伺服器模型 下面先看一下乙個客戶 伺服器模型的框架圖。可以看到,伺服器...