TCP
#include <stdio.h>
#include "/home/ubuntu/head.h"
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.124.85"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建流式套接字
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd=%d __%d__\n",cfd,__LINE__);
//填充服务器的地址信息,给connect函数去连接
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr =inet_addr(IP);
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
printf("bind success __%d__\n",__LINE__);
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
//向服务器发送数据
printf("请输入>>> ");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(send(cfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success __%d__\n", __LINE__);
//客服端
bzero(buf,sizeof(buf));
res = recv(cfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s : %d]服务器下线 __%d__\n",\
inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),__LINE__);
break;
}
printf("[%s : %d] %s __%d__\n",\
inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),buf,__LINE__);
}
//关闭所有文件描述符
if(close(cfd)<0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
.c
#include <stdio.h>
#include "/home/ubuntu/head.h"
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.124.85"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd=%d __%d__\n",sfd,__LINE__);
//允许端口快速被复用---监测端口号是否真的有进程在占用,如果没有,则快速复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
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);
//绑定服务器自身的地址信息
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("binf");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//讲套接字设置为被动接听状态
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
//获取连接成功的客户端信息,生成一个新的套接字
int newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("newfd=%d 客服端连接成功 __%d__\n",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)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s : %d]newfd=%d 客服端下线 __%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__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)
{
ERR_MSG("send");
return -1;
}
printf("send success __%d__\n",__LINE__);
}
//关闭所有文件描述符
close(newfd);
if(close(sfd)<0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
?UDP
#include <stdio.h>
#include "/home/ubuntu/head.h"
#define IP "192.168.124.85"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd=%d __%d__\n",sfd,__LINE__);
//定义并填充地址信息结构体,给服务绑定使用
//真实的地址信息结构体根据地址族指定
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr =inet_addr(IP);
//绑定服务器自身的地址信息
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("binf");
return -1;
}
printf("bind success __%d__\n",__LINE__);
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
//接受客服端数据
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
if(res<0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s : %d: %s __%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf,__LINE__);
//向客户端发送数据
strcat(buf,"^_^");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success __%d__\n",__LINE__);
}
//关闭所有文件描述符
if(close(sfd)<0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
#include <head.h>
#include <sys/types.h>
#define POT 2048
#define IP "192.168.124.32"
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("套接字创建成功 __%d__\n",__LINE__);
//定义填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须为AF_INET
sin.sin_port = htons(POT); //端口号
sin.sin_addr.s_addr = inet_addr(IP); // 本机IP
//定义结构体接受客户端的地址信息
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
char buf[128]="";
ssize_t res = 0;
while(1)
{
//向服务器发送数据
printf("请输入数据:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(cin)) < 0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功 __%d__\n",__LINE__);
bzero(buf,sizeof(buf));
//接受服务器数据
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
}
//关闭所有文件描述符
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
?