/*1,计算机的名字 IP 表示
2, 协议(语义+ 语法+ 规则)不同层 不同协议
IOS 七层模型
7 , 应用层 application
6 , 表示层 Presentation
5 会话层 主机之间的通信 session
4 传输层 transport
3 网络层 network 找出最佳的传输线路
2 数据链路层 data link 介质访问
1 物理层 physical 二进制传输 在通信信道上如何传输比特流
all people seem to need data process */
#include <Winsock2.h>
#include <stdio.h>
void main() {
WORD wVersionRequested;
WSADATA wsaData; int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable */ /* WinSock DLL. */ return; } /*
Confirm that the WinSock DLL supports 2.2.*/ /* Note that if the DLL supports versions greater */ /* than 2.2 in addition to 2.2, it will still return */ /* 2.2 in wVersion since that is the version we */ /* requested. */
if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 )
{ /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup( ); return;
} /* The WinSock DLL is acceptable. Proceed. */
SOCKET sockSrv = socket(AF_INET,SOCK_STREAM, 0 );
SOCKADDR_IN addrSrv ;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);// 主机字节序 转换成网络字节序
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
bind(sockSrv,(SOCKADDR *)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,5);
SOCKADDR_IN addrClient ;
int len = sizeof(SOCKADDR);
while(1) {
SOCKET sockConn = accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendBuf[100];
sprintf(sendBuf,"welcome %s to http://www.sunxin.org", inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[100];
recv(sockConn, recvBuf,100,0);
printf("%s /n", recvBuf);
closesocket (sockConn);
}
}
-------------------------------------
#include <Winsock2.h> #include <stdio.h> /*1,计算机的名字 IP 表示 2, 协议(语义+ 语法+ 规则)不同层 不同协议 IOS 七层模型 7 , 应用层 application 6 , 表示层 Presentation 5 会话层 主机之间的通信 session 4 传输层 transport 3 网络层 network 找出最佳的传输线路 2 数据链路层 data link 介质访问 1 物理层 physical 二进制传输 在通信信道上如何传输比特流 all people seem to need data process */ #include <Winsock2.h> #include <stdio.h>
void main() { //加载套接字 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ return; } /* Confirm that the WinSock DLL supports 2.2.*/ /* Note that if the DLL supports versions greater */ /* than 2.2 in addition to 2.2, it will still return */ /* 2.2 in wVersion since that is the version we */ /* requested. */ if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { /* Tell the user that we could not find a usable */ /* WinSock DLL. */ WSACleanup( ); return; } SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0); SOCKADDR_IN addrSrv; addrSrv.sin_family = AF_INET; addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); // 点分十进制的字符串格式 转换成一个 ulong 类型
addrSrv.sin_port = htons(6000);// 吧一个u-SHORT 类型的值从主机字节序顺序转换为tcp/ip 网络字节序
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
char recvBuf[100]; recv(sockClient,recvBuf,100,0); printf("%s/n",recvBuf); send(sockClient,"this is lili", strlen("this is lili")+1, 0); closesocket(sockClient); WSACleanup();
}