TcpSrv.cpp
#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
void main(){
//1、初始化套接字库 相当于分配网络资源
WSADATA wsaData;
int err = WSAStartup(0x0101, &wsaData);
if(err!=0){
return;
}
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup();
return;
}
//2、创建套接字
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);
if(INVALID_SOCKET==sockSrv){
WSACleanup();
return;
}
//3、绑定套接字到6000端口,注意网络字节序
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_port = htons(6000);
addrSrv.sin_family = AF_INET;
int x = sizeof(SOCKADDR);
int y = sizeof(SOCKADDR_IN);
err = bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
//4、开始监听客户端
listen(sockSrv, SOMAXCONN);
//5、循环处理每个监听到客户端的请求
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
while(1){
printf("6000开始监听......\n");
//5.1、接收客户端请求,得到客户端信息于addrClient中
SOCKET sockConn = accept(sockSrv, (SOCKADDR*)&addrClient,&len);
//5.2、向该客户端发送数据
char sendBuf[100];
//char FAR * inet_ntoa( struct in_addr in):
//将一个IP转换成一个互联网标准点分格式的字符串。
sprintf(sendBuf, "网管:%s,欢迎光临605聊天室..", inet_ntoa(addrClient.sin_addr));
send(sockConn, sendBuf, strlen(sendBuf)+1, 0);
//5.3、接收客户端数据
char recvBuf[100];
int nRecv = recv(sockConn, recvBuf, 100, 0);
if(nRecv>0){
recvBuf[nRecv]='\0';
printf("%s\n",recvBuf);
}
//5.4、关闭当前客户端连接
closesocket(sockConn);
}
//6、关闭服务端连接
closesocket(sockSrv);
//7、释放套接字库资源
WSACleanup();
}
TcpClient.cpp
#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
void main(){
//1、初始化套接字库 相当于分配网络资源
WSADATA wsaData;
//请求套接字 1.1 版本
WORD wVersionRequested = MAKEWORD(1,1);
int err = WSAStartup(wVersionRequested, &wsaData);
if(err!=0){
return;
}
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup();
return;
}
//2、创建套接字
SOCKET sockConn = socket(AF_INET, SOCK_STREAM, 0);
if(INVALID_SOCKET==sockConn){
WSACleanup();
return;
}
//3、向服务器端发起连接请求
SOCKADDR_IN addrSrv;
//in_addr_t inet_addr(const char *cp);
//将一个点分十进制的IP转换成一个长整数型数(u_long类型)
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
connect(sockConn,(SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
//4、接收服务器端数据
char recvBuf[100];
int nRecv = recv(sockConn, recvBuf, 100, 0);
if(nRecv>0){
recvBuf[nRecv]='\0';
printf("%s\n",recvBuf);
}
//5、发送数据
send(sockConn,"李四:网管你好,我是李四", strlen("李四:网管你好,我是李四"),0);
//6、关闭连接,本次客户请求完成
closesocket(sockConn);
//7、释放套接字库资源
WSACleanup();
int i;
scanf("%d",&i);
}
UdpSrv.cpp
#include "stdafx.h"
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
//加载套接字库
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if(err!=0)
return 0;
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup();
return 0;
}
//创建套接字
SOCKET sockSrv = socket(AF_INET, SOCK_DGRAM, 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));
//等待并接受数据
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
char recvBuf[100];
recvfrom(sockSrv, recvBuf,100,0,(SOCKADDR*)&addrClient,&len);
printf("%s\n",recvBuf);
//关闭套接字
closesocket(sockSrv);
WSACleanup();
int i;
scanf("%d",&i);
return 0;
}
UdpClient.cpp
#include "stdafx.h"
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
//加载套接字库
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if(err!=0)
return 0;
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup();
return 0;
}
//创建套接字
SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
//发送数据
sendto(sockClient,"hello", strlen("hello")+1,0,(SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
//关闭套接字
closesocket(sockClient);
WSACleanup();
int i;
scanf("%d",&i);
return 0;
}
////////////////////////////////////////////////////////////////////
基于UDP简单的聊天程序
/////////////////////////////////////////////////////////////////////
UdpSrv.cpp
#include "stdafx.h"
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
//加载套接字库
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if(err!=0)
return 0;
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup();
return 0;
}
//创建套接字
SOCKET sockSrv = socket(AF_INET, SOCK_DGRAM, 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));
char recvBuf[100];
char sendBuf[100];
char tempBuf[200];
//等待并接受数据
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1){
//接受数据
recvfrom(sockSrv, recvBuf,100,0,(SOCKADDR*)&addrClient,&len);
if('q'==recvBuf[0]){
//退出
sendto(sockSrv,"q",strlen("q")+1,0,(SOCKADDR*)&addrClient,len);
printf("Chat end!\n");
break;
}
sprintf(tempBuf, "%s say: %s", inet_ntoa(addrClient.sin_addr),recvBuf);
printf("%s\n",tempBuf);
printf("Please input data:\n");
gets(sendBuf);
//发送数据
sendto(sockSrv,sendBuf,strlen(sendBuf)+1,0,(SOCKADDR*)&addrClient,len);
}
//关闭套接字
closesocket(sockSrv);
WSACleanup();
int i;
scanf("%d",&i);
return 0;
}
UdpClient.cpp
#include "stdafx.h"
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
//加载套接字库
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if(err!=0)
return 0;
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1){
WSACleanup();
return 0;
}
//创建套接字
SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
char recvBuf[100];
char sendBuf[100];
char tempBuf[200];
int len=sizeof(SOCKADDR);
while(1){
//发送数据
printf("Please input data:\n");
gets(sendBuf);
sendto(sockClient, sendBuf, strlen(sendBuf)+1,0,(SOCKADDR*)&addrSrv, len);
//接受数据
recvfrom(sockClient, recvBuf,100,0,(SOCKADDR*)&addrSrv, &len);
if('q'==recvBuf[0]){
//退出
sendto(sockClient,"q",strlen("q")+1,0,(SOCKADDR*)&addrSrv,len);
printf("Chat end!\n");
break;
}
sprintf(tempBuf,"%s say:%s", inet_ntoa(addrSrv.sin_addr), recvBuf);
printf("%s\n", tempBuf);
}
//关闭套接字
closesocket(sockClient);
WSACleanup();
int i;
scanf("%d",&i);
return 0;
}