Linux系统编程——套接字
Linux
C语言
函数,指针,结构体
个人日常学习代码存档
#include
#include
#include
//#include
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
int c_fd;
int n_read;
char readBuf[128];
char *msg = "msg from client";
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.socket
c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1)
{
perror("socket");
exit(-1);
}
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2]));
inet_aton(argv[1],&c_addr.sin_addr);
//2.connect
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr)) == -1)
{
perror("connect");
exit(-1);
}
//3.send
write(c_fd,msg,strlen(msg));
//4.read
n_read = read(c_fd,readBuf,128);
if(n_read == -1)
{
perror("read");
}
else
{
printf("get message:%d,%s\n",n_read,readBuf);
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
//#include
int main(int argc,char **argv)
{
int c_fd;
int n_read;
char readBuf[128];
// char *msg = "msg from client";
char msg[128] = {
0};
struct sockaddr_in c_addr;
//初始化内存
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.创建套接字 socket()
c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1) //判断是否错误
{
perror("socket");
exit(-1);
}
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2])); //设置端口号
inet_aton(argv[1],&c_addr.sin_addr); //设置IP地址
//2.客户端直接与服务器进行连接 connect()
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr)) == -1)
{
perror("connect"); //判断是否错误
exit(-1);
}
//printf("Connect to server successfully!");
//3.进行收发信息
//此处的while好像没有必要
while(1)
{
//创建子进程进行发信息
if(fork() == 0)
{
while(1)
{
memset(msg,0,sizeof(msg));
printf("Please input your message: ");
gets(msg);
write(c_fd,msg,strlen(msg));
}
}
//接受消息
while(1)
{
memset(readBuf,0,sizeof(readBuf));
n_read = read(c_fd,readBuf,128);
if(n_read == -1) //判断是否错误
{
perror("read");
}
else
{
printf("\n");
printf("Get message: %s\n",readBuf);
printf("please input your message: ");
}
}
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
//#include
int main(int argc,char **argv)
{
int c_fd;
int n_read;
char readBuf[128];
// char *msg = "msg from client";
char msg[128] = {
0};
struct sockaddr_in c_addr;
//初始化内存
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.创建套接字 socket()
c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1) //判断是否错误
{
perror("socket");
exit(-1);
}
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2])); //设置端口号
inet_aton(argv[1],&c_addr.sin_addr); //设置IP地址
//2.客户端直接与服务器进行连接 connect()
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr)) == -1)
{
perror("connect"); //判断是否错误
exit(-1);
}
//printf("Connect to server successfully!");
//3.进行收发信息
//此处的while好像没有必要
while(1)
{
//创建子进程进行发信息
if(fork() == 0)
{
while(1)
{
memset(msg,0,sizeof(msg));
printf("Please input your message: ");
gets(msg);
write(c_fd,msg,strlen(msg));
}
}
//接受消息
while(1)
{
memset(readBuf,0,sizeof(readBuf));
n_read = read(c_fd,readBuf,128);
if(n_read == -1) //判断是否错误
{
perror("read");
}
else
{
printf("\n");
printf("Get message: %s\n",readBuf);
printf("please input your message: ");
}
}
}
return 0;
}
#include
#include
#include
//#include
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
int s_fd;
int c_fd;
int n_read;
char readBuf[128];
char *msg = "I get your connect";
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1)
{
perror("socket");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));
inet_aton("argv[1]",&s_addr.sin_addr);
//2.bind
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen
listen(s_fd,10);
printf("test1\n");
//4.accept
int clen = sizeof(struct sockaddr_in);
while(1)
{
c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&clen);
if(c_fd == -1)
{
perror("accept");
}
printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
if(fork() == 0)
{
//5.read
n_read = read(c_fd,readBuf,128);
if(n_read == -1)
{
perror("read");
}
else
{
printf("get message:%d,%s\n",n_read,readBuf);
}
//6.write
write(c_fd,msg,strlen(msg));
break;
}
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
int s_fd;
int c_fd;
int n_read;
char readBuf[128];
char msg[128] = {
0};
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
//判断输入的参数是否符合要求
if(argc != 3)
{
printf("param is not input\n");
exit(-1);
}
//初始化内存,将某一块内存中的内容全部设置为指定的值
memset(&s_addr,0,sizeof(struct sockaddr_in));
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.创建套接字 socket()
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1) //判断错误
{
perror("socket"); //输出错误信息
exit(-1); //退出
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2])); //配置端口号
inet_aton("argv[1]",&s_addr.sin_addr); //配置IP地址
//2.为套接字添加信息 bind()
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.监听网络连接 listen()
listen(s_fd,10); //未连接之前会卡在这里
//4.接受连接请求 accept()
int clen = sizeof(struct sockaddr_in);
//5.开始收发信息
while(1)
{
c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&clen);
//判断错误
if(c_fd == -1)
{
perror("accept");
}
//连接成功,输出对方IP
printf("Get connect: %s\n",inet_ntoa(c_addr.sin_addr));
//创建一个子进程去收发消息
if(fork() == 0)
{
//子进程再创建一个子进程负责发消息
if(fork() == 0)
{
while(1)
{
memset(msg,0,sizeof(msg));
printf("Please input your message: ");
gets(msg);
write(c_fd,msg,strlen(msg));
}
}
//子进程负责收消息
while(1)
{
memset(readBuf,0,sizeof(readBuf));
n_read = read(c_fd,readBuf,128);
//判断错误
if(n_read == -1)
{
perror("read");
}
else
{
printf("\n");
printf("Get message: %s\n",readBuf);
printf("please input your message: ");
}
}
//break;
//此处需要考虑父进程要不要退出还是卡住
while(1);
}
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char **argv)
{
int s_fd;
int c_fd;
int n_read;
int mark = 0;
char readBuf[128];
char msg[128] = {
0};
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
//判断输入的参数是否符合要求
if(argc != 3)
{
printf("param is not input\n");
exit(-1);
}
//初始化内存,将某一块内存中的内容全部设置为指定的值
memset(&s_addr,0,sizeof(struct sockaddr_in));
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.创建套接字 socket()
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1) //判断错误
{
perror("socket"); //输出错误信息
exit(-1); //退出
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2])); //配置端口号
inet_aton("argv[1]",&s_addr.sin_addr); //配置IP地址
//2.为套接字添加信息 bind()
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.监听网络连接 listen()
listen(s_fd,10); //未连接之前会卡在这里
//4.接受连接请求 accept()
int clen = sizeof(struct sockaddr_in);
//5.开始收发信息
while(1)
{
c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&clen);
//判断错误
if(c_fd == -1)
{
perror("accept");
}
mark++;
//连接成功,输出对方IP
printf("Get connect: %s\n",inet_ntoa(c_addr.sin_addr));
//创建一个子进程去收发消息
if(fork() == 0)
{
//子进程再创建一个子进程负责发消息
if(fork() == 0)
{
while(1)
{
sprintf(msg,"Welcome No.%d client",mark);//格式化输出函数,将“”写入字符串msg中
write(c_fd,msg,strlen(msg));
sleep(3);
}
}
//子进程负责收消息
while(1)
{
memset(readBuf,0,sizeof(readBuf));
n_read = read(c_fd,readBuf,128);
//判断错误
if(n_read == -1)
{
perror("read");
}
else
{
printf("\n");
printf("Get message: %s\n",readBuf);
printf("please input your message: ");
}
}
//break;
//此处需要考虑父进程要不要退出还是卡住
while(1);
}
}
return 0;
}
#include
int main(int argc,char **argv)
{
int time = atoi(argv[1]);
for(int i=0;i<time;i++)
{
system("sl");
}
}
如果文章有帮助就点个赞哦,有问题可留言。