tcpserver
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define IP "192.168.8.135"
#define PORT 6666
int main(int argc, const char *argv[])
{
// 创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0); // ipv4,tcp,默认协议
if (sfd < 0)
{
perror("socket");
return -1;
}
// 允许端口快速重用
int reuse = 1;
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
perror("setsockopt");
return -1;
}
// 填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET; // 必须填AF_INET
sin.sin_port = htons(PORT); // 端口号,1024~49151
sin.sin_addr.s_addr = inet_addr(IP); // 本机IP地址,ifconfig
// 绑定ip到socket
if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
perror("bind");
return -1;
}
printf("wait connect... __%d__\n", __LINE__);
if (listen(sfd, 128) < 0)
{
perror("listen");
return -1;
}
printf("wait connect...");
// 创建地址结构体,用来储存连接成功的客户端信息
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
// 阻塞函数
int newfd = accept(sfd, (struct sockaddr *)&cin, &addrlen);
if (newfd < 0)
{
perror("accept");
return -1;
}
printf("[%s:%d] newfd=%d 连接成功__%d__\n",
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);
char buf[128] = "";
ssize_t res = 0;
while (1)
{
bzero(buf, sizeof(buf));
// 接受
res = recv(newfd, buf, sizeof(buf), 0);
if (res < 0)
{
perror("recv");
return -1;
}
else if (0 == res)
{
printf("[%s:%d] newfd=%d 客户端下线__%d__\n",
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf, __LINE__);
break;
}
printf("[%s:%d] newfd=%d :%s __%d__\n",
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf, __LINE__);
// 发送 将数据拼接字符串后发送回去
strcat(buf, "*****");
if (send(newfd, buf, sizeof(buf), 0) < 0)
{
perror("send");
return -1;
}
}
close(newfd);
close(sfd);
return 0;
}
tcpclient
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define IP "192.168.8.135"
#define PORT 6666
int main(int argc, const char *argv[])
{
// 创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
{
perror("socket");
return -1;
}
// 不一定需要绑定
// connect
struct sockaddr_in sin;
sin.sin_family = AF_INET; // 族相同
sin.sin_addr.s_addr = inet_addr(IP); // 地址要转换成网络字节序inte_addr
sin.sin_port = htons(PORT); // 端口要转换成点分十进制htons
socklen_t len = sizeof(sin);
// 客户端
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if (cfd < 0)
{
perror("socket");
}
printf("cfd = %d\n", cfd);
// 连接服务器和客户端cfd是服务器地址
int newfd = connect(cfd, (struct sockaddr *)&sin, len);
if (newfd < 0)
{
perror("connect");
return -1;
}
printf("连接成功\n");
char buf[128] = "";
char str[128] = "";
int res = 0;
while (1)
{
// send
memset(buf, 0, sizeof(buf));
printf("请输入:\n");
scanf("%s", buf);
res = send(cfd, buf, sizeof(buf), 0);
if (res < 0)
{
perror("send");
return -1;
}
// recv
memset(str, 0, sizeof(str));
res = recv(cfd, str, sizeof(str), 0);
if (res < 0)
{
perror("recv");
return -1;
}
printf("read: %s\n", str);
}
close(sfd);
close(newfd);
return 0;
}
udpserver
main.c
#include <head.h>
#define ERR_MSG(msg) do{\
perror(msg);\
printf("__%d__\n",__LINE__);\
}while(0);
#define IP "192.168.8.135"
#define PORT 6666
int main(int argc,const char * argv[])
{
//创建套接字
printf("服务器\n");
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
ERR_MSG("socket");
return -1;
}
printf("套接字创建成功\n");
//绑定bind
struct sockaddr_in sin;
sin.sin_family = AF_INET; //族需要相同
sin.sin_port = htons(PORT); //端口转变成点分十进制
sin.sin_addr.s_addr = inet_addr(IP); //地址转成网络字节序
socklen_t len = sizeof(sin);
if((bind(sfd,(struct sockaddr*)&sin,len)) > 0 ){
ERR_MSG("bind");
return -1;
}
printf("绑定bind成功\n");
struct sockaddr_in cin;
socklen_t len1 = sizeof(cin);
char buf[128] = "";
char str[128] = "";
int res = 0;
while(1){
memset(buf,0,sizeof(buf));
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&len1);
if(res < 0){
ERR_MSG("recvfrom");
return -1;
}
printf("read:-->[%s]:[%d]%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
memset(str,0,sizeof(str));
printf("请输入:\n");
scanf("%s",str);
res = sendto(sfd,str,sizeof(str),0,(struct sockaddr*)&cin,len1);
if(res < 0){
ERR_MSG("sendto");
}
}
return 0;
}
udpclinet
main.c
#include <head.h>
#define ERR_MSG(msg) do{\
perror(msg);\
printf("__%d__\n",__LINE__);\
}while(0);
#define IP "192.168.8.135"
#define PORT 6666
int main(int argc,const char * argv[])
{
printf("客户端\n");
//创建报式套接字
int sdf = socket(AF_INET,SOCK_DGRAM,0);
if(sdf < 0){
ERR_MSG("socket");
return -1;
}
printf("报式套接字创建成功\n");
//不一定需要绑定
//发送之前的准备
struct sockaddr_in sin;
sin.sin_family = AF_INET; //族连接在一起
sin.sin_port = htons(PORT); //端口转换成点分十进制
sin.sin_addr.s_addr = inet_addr(IP); //地址转变成网络字节序
socklen_t len = sizeof(sin);
//保存的信息及长度
struct sockaddr_in cin;
socklen_t len1 = sizeof(cin);
char buf[128] = "";
char str[128] = "";
int res = 0;
while(1){
memset(buf,0,sizeof(buf));
printf("请输入:\n");
scanf("%s",buf);
res = sendto(sdf,buf,sizeof(buf),0,(struct sockaddr*)&sin,len);
if(res < 0){
ERR_MSG("sendto");
return -1;
}
memset(str,0,sizeof(str));
res = recvfrom(sdf,str,sizeof(str),0,(struct sockaddr*)&cin,&len1);
if(res < 0){
ERR_MSG("recvfrom");
return -1;
}
printf("read:-->[%s]:[%d]%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),str);
}
return 0;
}