TCP
服務器端
#include <myhead.h>
#define ?SER_PORT ?8888 ? ? ? ?//服務器端口號
#define SER_IP ?"192.168.108.179" ? //服務器IP地址?
int main(int argc, const char *argv[])
{
//創建一個用于連接的套接字文件描述符
int sfd ?= socket(AF_INET, SOCK_STREAM, 0);
if(-1 == sfd)
{
perror("socket ?error");
return -1;
}
?? ?printf("socket 成功 sfd = %d\n", sfd);
//封裝地址信息結構體變量
struct sockaddr_in sin; ? ? ? ?//地址信息結構體變量
sin.sin_family = AF_INET; ? ? ?//IPV4
sin.sin_port = htons(SER_PORT); ? //端口號,主機轉網絡字節序
sin.sin_addr.s_addr = inet_addr(SER_IP); ?//ip地址轉網絡字節序
//給套接字綁定ip地址和端口號
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) ==-1)
{
perror("bind ?error");
return -1;
}
printf("bind success\n");
//將套接字啟動監聽
if(listen(sfd, 128) == -1)
{
perror("listen ?error");
return -1;
}
printf("listen success\n");
?? ?//阻塞等等客戶端的連接,如果有新客戶端連接,則創建一個用于通信的新套接字
struct sockaddr_in cin; ? ? ? ? ? ?//用于接受客戶端套接字信息
socklen_t addrlen = sizeof(cin); ? //用于接受客戶端套接字的長度
?? ?int new_fd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(-1 == new_fd)
{
perror("accept error");
return -1;
}
printf("[%s:%d]發來連接,new_fd = %d\n", inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),new_fd);
?? ?//使用新套接字跟客戶端進行通信
while(1)
{
//從套接字中讀取消息
char rbuf[128] = ""; ? ? ? ? ?//存放接受消息的容器
int res = recv(new_fd, rbuf, sizeof(rbuf), 0);
if(res == 0)
{
printf("客戶端已下線\n");
close(new_fd);
break;
}
if(res == -1)
{
perror("recv?error");
close(new_fd);
close(sfd);
return -1;
}
printf("[%s:%d] : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), rbuf);
?? ??? ?//給當前消息加個笑臉還回去
strcat(rbuf, "*_*");
?? ??? ?//向套接字中寫入消息
send(new_fd, rbuf, strlen(rbuf), 0);
printf("發送成功\n");
close(new_fd);
?? ?}
?? ?//關閉監聽套接字
close(sfd);
?? ?
return 0;
}
客戶端
#include <myhead.h>
#define ?SER_PORT 8888 ? ? ? ? ? ? ? //服務器端口號
#define SER_IP "192.168.108.179" ? ? //服務器ip地址
#define CLIENT_PORT 9999 ? ? ? ? ? ? //客戶端端口號
#define CLIENT_IP "192.168.108.179" //客戶端ip地址
int main(int argc, const char *argv[])
{
//創建一個用于通信的套接字文件描述符
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == cfd)
{
perror("socket error");
return -1;
}
printf("socket success ? cfd = %d\n", cfd);
//封裝服務器的地址信息結構體
struct sockaddr_in sin;
sin.sin_family = AF_INET; ? ? ? ? ? ?//通信域
sin.sin_port = htons(SER_PORT); ? ? //服務器端口號
sin.sin_addr.s_addr = inet_addr(SER_IP); ? ?//服務器ip地址
//連接服務器
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("connect ?error");
return -1;
}
printf("連接成功\n");
?? ?
//數據收發
while(1)
{
char wbuf[128] = ""; ? ? ? ? ? ?//用于發送數據的容器
fgets(wbuf, sizeof(wbuf), stdin); ? ?//從終端獲取消息
wbuf[strlen(wbuf)-1] = '\0'; ? ? ? ? //將換行改成'\0'
if(strcmp(wbuf, "quit") == 0)
{
break;
}
?? ??? ?//將數據發送給服務器
send(cfd, wbuf, strlen(wbuf), 0);
printf("發送成功\n");
?? ??? ?//接收服務器發送的消息
recv(cfd, wbuf, sizeof(wbuf), 0);
printf("收到的消息為:%s\n", wbuf);
}
//關閉套接字
close(cfd)
return 0;
UDP
服務器端
#include <myhead.h>
#define SER_IP "192.168.108.179"
#define SER_PORT 8888? ? ? ??
int main(int argc, const char *argv[])
{
//創建服務器套接字文件描述符
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(-1 == sfd)
{
perror("socket error");
return -1;
}
printf("socket success ?sfd = %d\n", sfd);? ?
填充地址信息結構體
struct sockaddr_in sin;
sin.sin_family = AF_INET; ? ? ? ? ?//配置通信域
sin.sin_port = htons(SER_PORT); ? ? ?//配置端口號
sin.sin_addr.s_addr = inet_addr(SER_IP); ? //配置ip地址
//綁定操作
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) == -1)
{
perror("bind ?error");
return -1;
}
printf("bind ?success\n");
?? ?//數據收發
//定義用于接收對端地址信息結構體
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin); ? ? //接收地址信息的大小
?? ?while(1)
{
char rbuf[128] = ""; ? ? ? ? //定義接收消息的容器
//接收客戶端消息
recvfrom(sfd, rbuf, sizeof(rbuf), 0, (struct sockaddr*)&cin, &addrlen);
printf("[%s:%d] : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), rbuf);
?? ??? ?//給消息加個笑臉還回去
strcat(rbuf, "*_*");
?? ??? ?//向客戶端發送消息
sendto(sfd, rbuf, strlen(rbuf), 0, (struct sockaddr*)&cin, sizeof(cin));
printf("發送成功\n");
?? ?}
?? ?//4、關閉服務器
close(sfd);
return 0;
}
客戶端
#include<myhead.h>
#define SER_IP "192.168.108.179" ? ? ? //服務器ip地址
#define SER_PORT 8888 ? ? ? ? ? ? ? ? ?//服務器端口號
#define CLI_IP ?"192.168.108.179" ? ? ?//客戶端ip地址
#define CLI_PORT 9999 ? ? ? ? ? ? ? ? ?//客戶端端口號
int main(int argc, const char *argv[])
{
//創建用于通信的套接字文件描述符
int cfd = socket(AF_INET, SOCK_DGRAM, 0);
if(-1 == cfd)
{
perror("socket error");
return -1;
}
printf("socket success ?cfd = %d\n", cfd);
填充客戶端地址信息結構體
struct sockaddr_in cin;
cin.sin_family = AF_INET; ? ? ? ? ? ? ? ?//通信域
cin.sin_port = htons(CLI_PORT); ? ? ? ? ?//客戶端端口號
cin.sin_addr.s_addr = inet_addr(CLI_IP); ?//客戶端ip地址
//進行綁定工作
if(bind(cfd, (struct sockaddr*)&cin, sizeof(cin)) == -1)
{
perror("bind error");
return -1;
}
printf("bind ?success\n");
?? ?//數據收發
//封裝服務器的地址信息結構體
struct sockaddr_in sin; ? ? ? ? ? ? //服務器地址信息結構體
sin.sin_family = AF_INET;
sin.sin_port = htons(SER_PORT); ? ? ? ? //服務器端口號
sin.sin_addr.s_addr = inet_addr(SER_IP); ?//服務器ip地址
socklen_t addrlen = sizeof(sin); ? ? ? ? ?//地址信息結構體的大小
while(1)
{
char wbuf[128] = "";
//從終端獲取一個字符串
fgets(wbuf, sizeof(wbuf), stdin);
wbuf[strlen(wbuf)-1] = 0;
?? ??? ?//將消息發送給服務器
sendto(cfd, wbuf, strlen(wbuf), 0, (struct sockaddr*)&sin, addrlen);
printf("發送成功\n");
?? ??? ?//接收服務器回復的消息
recv(cfd, wbuf, sizeof(wbuf), 0,NULL,NULL);
printf("收到消息為:%s\n", wbuf);
?? ?}
?? ?//4、關閉客戶端
close(cfd);
return 0;
}