#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
//1.创建套接字
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
//网际协议,套接字类型TCP选择流式套接字
if(-1 == sock_fd)
{
perror("creat sock_fd failed");
return -1;
}
//2.绑定服务器地址
struct sockaddr_in serve;
serve.sin_family = AF_INET;//地址族成员
serve.sin_port = htons(atoi(argv[2]));//0-------65535 (0----5000,大型企业,5000-----10000,留给将来的大型企业,10000-----65535,才是留给我们的)
serve.sin_addr.s_addr = inet_addr(argv[1]);//需要把点分式字符串转换为无符号的32位网络地址in_addr_t,
//绑定函数
int ret = bind(sock_fd, (struct sockaddr *)&serve, sizeof(serve));
if(ret == -1)
{
perror("bind failed");
}
//3.发起连接(connect)
connect(sock_fd,(struct sockaddr *)&serve,sizeof(serve));
char *buf = malloc(100);
while(1)
{
memset(buf,0,100);
fgets(buf,100,stdin);
//4.发送消息(send)
send(sock_fd,buf,100,0);
if(strcmp(buf,"bye\n")==0)
{
break;
}
}
close(sock_fd);
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <pthread.h>
void *pthread(&sr) //线程例程,必须是一个指针函数,并且参数为万能指针
{
if (access("./myfifo1",F_OK))//判断文件是否存在,存在返回-1,否则返回0
{
mkfifo("./myfifo1",0666);
}
int fd = open("./myfifo1",O_RDONLY);
char buf[100];
printf("等待连接....\n");
int acc_fd = accept(sock_fd, NULL, NULL);//阻塞等待
printf("连接成功!!!\n");
//打印IP和端口号
printf("ip=%s port=%d",sr.sa_data[14],sr.sa_family)
}
int main(int argc,char **argv)//主函数传参,(先传IP再传端口)argv[1]IP,ARGV[2]端口号
{
//1.创建套接字
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
//网际协议,套接字类型TCP选择流式套接字
if(-1 == sock_fd)
{
perror("creat sock_fd failed");
return -1;
}
//2.绑定服务器的IP地址+端口号
//定义结构体
struct sockaddr_in serve;
serve.sin_family = AF_INET;//地址族成员
serve.sin_port = htons(atoi(argv[2]));//0-------65535 (0----5000,大型企业,5000-----10000,留给将来的大型企业,10000-----65535,才是留给我们的)
serve.sin_addr.s_addr = inet_addr(argv[1]);//需要把点分式字符串转换为无符号的32位网络地址in_addr_t,
//绑定函数
int ret = bind(sock_fd, (struct sockaddr *)&serve, sizeof(serve));
if(ret == -1)
{
perror("bind failed");
}
//3.监听
int listen_ret = listen(sock_fd, 4);//可以设置监听的套接字的最大个数
//创建线程
pthread_t tid;
pthread_create(&tid,NULL,pthread,NULL);//创建标准属性线程,可结合,静态优先级为0
//定义结构体
struct sockaddr sr;
sr.sa_family = argv[1];
sr.sa_data[14] = htons(atoi(argv[2]));
if (access("./myfifo",F_OK))//判断文件是否存在,存在返回-1,否则返回0
{
mkfifo("./myfifo",0666);
}
int fd = open("./myfifo",O_WRONLY);
char buf[100];
while(1)
{
memset(buf,0,100);
//接收消息
recv(acc_fd, buf, 100, 0);
printf("客户端:%s\n",buf);
if(strcmp(buf,"bye\n")==0)
{
break;
}
}
close(sock_fd);
close(acc_fd);
ls版:
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
int con_fd = -1;
/*tcp协议客户端服务器CS结构的通信(打印客户端的IP和端口号,客户端能够多次运行)*/
void *nv_shen(void *arg)
{
int fd = con_fd;
struct sockaddr_in addr = *(struct sockaddr_in *)arg;
char *buf = malloc(100);
while(1)
{
memset(buf,0,100);
int n = recv(fd,buf,100,0);//recv和send配套阻塞
if(n == 0)
{
printf("【端口号:[%d],%s】 : 已退出\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr));
break;
}
printf("【端口号:[%d],%s】 : %s\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr),buf );
}
}
int main(int argc,char **argv)
{
//1.创建套接字
int sock_fd = socket(AF_INET,SOCK_STREAM,0);
//2.绑定
struct sockaddr_in serve_addr, cilent_addr;//
serve_addr.sin_family = AF_INET;
serve_addr.sin_port = htons(atoi(argv[2]));//网络字节序-----主机字节序
serve_addr.sin_addr.s_addr = inet_addr(argv[1]);//网络字节序
socklen_t serve_len = sizeof(serve_addr);
socklen_t cilent_len = sizeof(cilent_addr);
bind(sock_fd, (struct sockaddr *)&serve_addr,serve_len);
//3.监听
listen(sock_fd,4);
//创建线程
pthread_t tid;
char *buf = malloc(100);
//5.接收消息
while(1)
{
//4.等待连接
printf("等待连接...\n");
con_fd = accept(sock_fd, (struct sockaddr *)&cilent_addr, &cilent_len);//阻塞运行
//有对端地址,怎么访问到?
cilent_addr.sin_port;
printf("端口号:[%d],%s连接成功...\n", ntohs(cilent_addr.sin_port), inet_ntoa(cilent_addr.sin_addr) );
pthread_create(&tid,NULL,nv_shen, (void *)&cilent_addr);
}
}
#include <stdio.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
int main(int argc,char **argv)
{
//1.创建套接字
int sock_fd = socket(AF_INET,SOCK_STREAM,0);
//2.绑定
struct sockaddr_in serve_addr;
serve_addr.sin_family = AF_INET;
serve_addr.sin_port = htons(atoi(argv[2]));//网络字节序-----主机字节序
serve_addr.sin_addr.s_addr = inet_addr(argv[1]);//网络字节序
socklen_t serve_len = sizeof(serve_addr);
bind(sock_fd, (struct sockaddr *)&serve_addr,serve_len);
//3.发起连接请求
connect(sock_fd,(struct sockaddr *)&serve_addr,serve_len);
char *buf = malloc(100);
while(1)
{
memset(buf,0,100);
//4.发消息
fgets(buf,100,stdin);
send(sock_fd,buf,100,0);
}
}