服務(wù)器
#include
#include
#include
#include
#pragma comment(lib,"WS2_32.lib")
int main(){
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 0);
::WSAStartup(sockVersion, &wsaData);//第一個(gè)參數(shù)是WINSOCK版本號(hào)甩卓,第二個(gè)參數(shù)是指向WSADATA的指針
/*創(chuàng)建服務(wù)器的套接字*/
SOCKET s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//三個(gè)參數(shù)分別代表使用TCP/IP割以,傳輸過程使用TCP墩崩,不使用其他特殊協(xié)議
if (s == INVALID_SOCKET)
{
printf("Failed socket()\n");
::WSACleanup();
system("pause");
return 0;
}
/*socket中裝入地址信息*/
sockaddr_in sin;
sin.sin_family = AF_INET;//sin_family指代協(xié)議族,在socket編程中只能是AF_INET
sin.sin_port = htons(8888);//表示服務(wù)器監(jiān)聽的端口號(hào)為13
sin.sin_addr.S_un.S_addr = INADDR_ANY;//儲(chǔ)存IP地址馋吗,INADDR_ANY就是指定地址為0.0.0.0的地址,
//這個(gè)地址事實(shí)上表示不確定地址秋秤,或“所有地址”宏粤、“任意地址”
/*綁定地址及端口號(hào)*/
if (::bind(s, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("Falied bind()\n");
::WSACleanup();
system("pause");
return 0;
}
/*監(jiān)聽客戶端的連接請求*/
if (::listen(s, 2) == SOCKET_ERROR)
{
printf("Falied bind()\n");
::WSACleanup();
system("pause");
return 0;
}
sockaddr_in remoteAddr;
int nAddrLen = sizeof(remoteAddr);
SOCKET client;
time_t t = time(0);
char tmp[64];
char po[256];
//strftime(tmp, sizeof(tmp), "%Y/%m/%d%X%A\n\t", localtime(&t)); //提取系統(tǒng)時(shí)間
/*循環(huán)接受連接請求*/
while (TRUE)
{
client = ::accept(s, (SOCKADDR*)&remoteAddr, &nAddrLen);//accept默認(rèn)會(huì)阻塞進(jìn)程,知道有一個(gè)客戶建立連接后返回灼卢,
//它返回的是一個(gè)新可用的套接字绍哎,這個(gè)套接字是連接套接字
if (client == INVALID_SOCKET)
{
printf("Failed accept()\n");
continue;
}
printf("接受到一個(gè)客戶端的連接\r\n\n" );
while(1){
printf("我:");
scanf("%s",&tmp);
//while(strlen(tmp)==0){
::send(client, tmp, strlen(tmp), 0);
int nRecv = ::recv(client, po, 256, 0);
if (nRecv > 0)
{
po[nRecv] = '\0';
printf("%s:%s\n", inet_ntoa(remoteAddr.sin_addr),po);
//system("pause");
}//}
}
::closesocket(client);/*關(guān)閉連接*/
}
::closesocket(s);/*關(guān)閉套接字*/
system("pause");
return 0;
}
客戶端
#include
#include
#include
#include
#pragma comment(lib,"WS2_32.lib")
int main(){
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 0);
::WSAStartup(sockVersion, &wsaData);//第一個(gè)參數(shù)是WINSOCK版本號(hào),第二個(gè)參數(shù)是指向WSADATA的指針
/*創(chuàng)建服務(wù)器的套接字*/
SOCKET s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//三個(gè)參數(shù)分別代表使用TCP/IP鞋真,傳輸過程使用TCP崇堰,不使用其他特殊協(xié)議
if (s == INVALID_SOCKET)
{
printf("Failed socket()\n");
::WSACleanup();
system("pause");
return 0;
}
/*socket中裝入地址信息*/
sockaddr_in servAddr;
servAddr.sin_family = AF_INET;//sin_family指代協(xié)議族,在socket編程中只能是AF_INET
servAddr.sin_port = htons(8888);//表示服務(wù)器監(jiān)聽的端口號(hào)為13
servAddr.sin_addr.S_un.S_addr = inet_addr("192.168.31.220");
/*綁定地址及端口號(hào)*/
if (::connect(s, (sockaddr*)&servAddr, sizeof(servAddr)) == -1);
{
printf("連接成功,等待消息...\n\n");
while(1){
char buff[256];
char te[256];
int nRecv = ::recv(s, buff, 256, 0);
if (nRecv > 0)
{
buff[nRecv] = '\0';
printf("對(duì)方:%s\n", buff);
printf("我:");
scanf("%s",&te);
::send(s, te, strlen(te), 0);
//system("pause");
}}
printf("Failed connect()\n");
::WSACleanup();
system("pause");
}
/*接收數(shù)據(jù)并打印到屏幕上*/
return 0;
}
2015.6.27