歡迎關注我的新博客https://pino-hd.github.io景东,最新的博文都會發(fā)布在上面哦~
前言
因為在寫代碼的時候感覺用VS太過臃腫,每次寫個程序還要創(chuàng)建一個項目谒养,并且特別占用空間桶蝎,因此安裝了mingw杠娱,可以在命令行中直接gcc編譯,特別方便与学,安利一下~
并且在編譯socket程序的時候彤悔,一定要加上-l wsock32
選項,因為mingw默認沒有包含windows庫
TCP版本
- SERVER端
#include <Winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <stdio.h>
int main(int argc, char* argv[]) {
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 2);
if (WSAStartup(sockVersion, &wsaData) != 0) {
return 0;
}
SOCKET sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sListen == INVALID_SOCKET) {
printf("socket error\n");
return 0;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(4500);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(sListen, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR) {
printf("socket error\n");
closesocket(sListen);
return 0;
}
if (listen(sListen, 5) == SOCKET_ERROR) {
printf("socket error\n");
closesocket(sListen);
return 0;
}
struct sockaddr_in remoteAddr;
SOCKET sClient;
int nAddrLen = sizeof(remoteAddr);
char revData[255];
while(TRUE){
sClient = accept(sListen, (SOCKADDR*)&remoteAddr, &nAddrLen);
if (sClient == SOCKET_ERROR) {
printf("accept() error\n");
continue;
}
printf("接受到一個連接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
int ret = recv(sClient, revData, 255, 0);
if (ret > 0) {
revData[ret] = 0x00;
printf(revData);
}
char* buff = (char*)"\r\nHello, my friend\r\n";
send(sClient, buff, strlen(buff), 0);
closesocket(sClient);
}
closesocket(sListen);
WSACleanup();
return 0;
}
- CLIENT端
#include <WinSock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char* argv[]) {
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 2);
if (WSAStartup(sockVersion, &wsaData) != 0) {
return 0;
}
SOCKET sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sClient == SOCKET_ERROR) {
printf("socket error\n");
return 0;
}
struct sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(4500);
servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if (connect(sClient, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) {
printf("socket error\n");
closesocket(sClient);
return 0;
}
char* buff = (char*)"\r\nHello, my friend\r\n";
send(sClient, buff, strlen(buff), 0);
char revData[255];
int ret = recv(sClient, revData, 255, 0);
if (ret > 0) {
revData[ret] = 0x00;
printf(revData);
}
closesocket(sClient);
WSACleanup();
return 0;
}
- 編譯
gcc server.c -o server -lwsock32
gcc client.c -o client -lwsock32
UDP版本
- SERVER端
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <stdio.h>
int main(int argc, char* argv[])
{
WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 2);
if (WSAStartup(sockVersion, &wsaData) != 0)
{
return 0;
}
SOCKET serSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (serSocket == INVALID_SOCKET)
{
printf("socket error !");
return 0;
}
struct sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(8000);
serAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(serSocket, (SOCKADDR*)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("bind error !");
closesocket(serSocket);
return 0;
}
struct sockaddr_in remoteAddr;
int nAddrLen = sizeof(remoteAddr);
while (TRUE)
{
char recvData[255];
int ret = recvfrom(serSocket, recvData, 255, 0, (SOCKADDR*)&remoteAddr, &nAddrLen);
if (ret > 0)
{
recvData[ret] = 0x00;
printf("接受到一個連接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
printf(recvData);
}
char * sendData = "一個來自服務端的UDP數據包\n";
sendto(serSocket, sendData, strlen(sendData), 0, (SOCKADDR*)&remoteAddr, nAddrLen);
}
closesocket(serSocket);
WSACleanup();
return 0;
}
- CLIENT端
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <stdio.h>
int main(int argc, char* argv[])
{
WORD socketVersion = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(socketVersion, &wsaData) != 0)
{
return 0;
}
SOCKET sclient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8000);
sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
int len = sizeof(sin);
char * sendData = "Hello, my friend\n";
sendto(sclient, sendData, strlen(sendData), 0, (SOCKADDR*)&sin, len);
char recvData[255];
int ret = recvfrom(sclient, recvData, 255, 0, (SOCKADDR*)&sin, &len);
if (ret > 0)
{
recvData[ret] = 0x00;
printf(recvData);
}
closesocket(sclient);
WSACleanup();
return 0;
}
- 編譯
gcc server_udp.c -o server_udp -lwsock32
gcc client_udp.c -o client_udp -lwsock32
總結
對于TCP版本的程序來說
server 端:創(chuàng)建WSAStartup -》檢測!=0 -》創(chuàng)建socket -》檢測SOCKET_ERROR -》聲明sockaddr_in結構 -》bind -》檢測SOCKET_ERROR -》listen -》檢測SOCKET_ERROR -》進入循環(huán) -》accept -》檢測SOCKET_ERROR -》接收索守、發(fā)送消息
client 端:創(chuàng)建WSAStartup -》檢測!=0 -》創(chuàng)建socket -》檢測SOCKET_ERROR -》聲明sockaddr_in結構 -》connect -》檢測SOCKET_ERROR -》發(fā)送晕窑、接收消息
對于UDP版本的程序來說
server 端:創(chuàng)建WSAStartup -》檢測!=0 -》創(chuàng)建socket -》檢測SOCKET_ERROR -》聲明sockaddr_in結構 -》bind -》檢測SOCKET_ERROR -》進入循環(huán) -》發(fā)送、接收消息
client 端:創(chuàng)建WSAStartup -》檢測!=0 -》創(chuàng)建socket -》檢測SOCKET_ERROR -》聲明sockaddr_in結構 -》發(fā)送卵佛、接收消息
TCP中杨赤,使用send和recv函數進行發(fā)送和接收消息
UDP中,使用sendto和recvfrom函數進行發(fā)送和接收消息