改改就是个小型局域网聊天

服务器端:

 // File Name: process_server.c
// Author: jiujue
// Created Time: 2019年03月10日 星期日 20时29分18秒 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include <unistd.h>
#include <sys/socket.h>
#include <ctype.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#include <pthread.h> typedef struct sock_info
{
int m_fd;
pthread_t m_pthid;
struct sockaddr_in m_addr; }Sock_info; void* works(void* arg)
{
Sock_info *c_info = (Sock_info*)arg; //obtain client info
int client_port = ntohs(c_info->m_addr.sin_port);
char client_Ip[]; socklen_t buf_len = sizeof(client_Ip); inet_ntop(AF_INET, (void*)&c_info->m_addr.sin_addr.s_addr,client_Ip,buf_len); printf("\t\t\t\t Ip %s, port %d,connect successful\n",client_Ip,client_port); while()
{
char buf[] = {};
int read_len = read(c_info->m_fd, buf, sizeof(buf));
if(read_len > )
{
buf[read_len+]='\n';
printf("->-> Obtain if of ,Ip %s, port %d, send info: %s\n",client_Ip,client_port,buf);
write(c_info->m_fd,buf,strlen(buf));
}
else if( == read_len)
{
printf("\t\t\t\t Ip %s, port %d, disconnect\n",client_Ip,client_port);
break;
}
else if(- == read_len)
{
perror("read error");
exit();
}
} return NULL;
} int main(int argc, const char* argv[])
{
if(argc < )
{
printf("eg: ./app IP Port\n");
exit();
}
short port = atoi(argv[]); int lfd = socket(AF_INET, SOCK_STREAM, );
if(- == lfd)
{
perror("socket error");
exit();
} struct sockaddr_in serv;
serv.sin_port = htons(port);
serv.sin_family = AF_INET;
inet_pton(AF_INET, argv[], &serv.sin_addr.s_addr); int res = bind(lfd, (struct sockaddr *)&serv, sizeof(serv));
if(- == res)
{
perror("bind error");
exit();
} res = listen(lfd, );
if(- == res)
{
perror("listen error");
exit();
} while()
{
printf("\a Wait accepting...\n");
struct sockaddr_in client_add; socklen_t len = sizeof(client_add); int cfd = accept(lfd,(struct sockaddr*)&client_add, &len); while(- == cfd && cfd == EINTR)
{
cfd = accept(lfd,(struct sockaddr*)&client_add, &len);
} // supply son pthread info
Sock_info* s_info =(Sock_info*)malloc(sizeof(Sock_info)); s_info->m_fd = cfd;
s_info->m_addr = client_add; int res = pthread_create(&s_info->m_pthid, NULL, works, (void*)s_info);
if(res == -)
{
perror("pthread_creat error");
exit();
}
pthread_detach(s_info->m_pthid); } close(lfd);
return ;
}

客户端:

 // File Name: socket_client.c
// Author: jiujue
// Created Time: 2019年03月09日 星期六 13时00分05秒 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h> int main(int argc, const char* argv[])
{ if(argc < )
{
printf("eg: ./app Ip Port\n");
exit();
}
int port = atoi(argv[]); int fd = socket(AF_INET, SOCK_STREAM, );
if(- == fd)
{
perror("socket error");
exit();
} struct sockaddr_in serv; serv.sin_port = htons(port);
serv.sin_family = AF_INET;
inet_pton(AF_INET, argv[], &serv.sin_addr.s_addr); socklen_t len = sizeof(serv);
int res = connect(fd,(struct sockaddr *) &serv, len);
if(- == res)
{
perror("connect error");
exit();
}
printf("Connectt server successful!!\n"); while()
{
printf("Please input string\n>>");
char buf[];
fgets(buf,sizeof(buf),stdin);
write(fd, buf, strlen(buf));
printf("send buf: %s\n",buf);
len = read(fd,buf,(buf));
if(len > )
{
printf("Recv buf: %s\n",buf);
}else if(len == )
{
printf("Serer disconnect \n");
break;
}
else if(- == len)
{
perror("Read errror");
exit();
}else
{
printf("I no now\n");
}
} close(fd);
return ;
}

结语:有问题欢迎提在下方 ,本人在校学生,时间较为充裕, 有时间会回复的。

/* 原创文章 转载请附上原链接: https://www.cnblogs.com/jiujue/p/10513859.html   */

ubuntu环境下实现 多线程的socket(tcp) 通信的更多相关文章

  1. ubuntu环境下docker安装步骤

    本文是根据docker官方文档翻译,原文:https://docs.docker.com/engine/installation/linux/ubuntulinux/ Docker 支持以下 Ubun ...

  2. Linux(Ubuntu)环境下使用Fiddler

    自己的开发环境是Ubuntu, 对于很多优秀的软件但是又没有Linux版本这件事,还是有点遗憾的.比如最近遇到一个问题,在分析某个网站的请求路径和cookie时就遇到了问题.本来Chome浏览器自带的 ...

  3. 多线程编程之Linux环境下的多线程(三)

    前面两篇文章都讲述了Linux环境下的多线程编程基础知识,也附带了典型实例.本文主要比较一下Linux环境与Windows环境下的多线程编程区别. 看待技术问题要瞄准其本质,不管是WIN32.Linu ...

  4. 多线程编程之Linux环境下的多线程(一)

    一.Linux环境下的线程 相对于其他操作系统,Linux系统内核只提供了轻量级进程的支持,并未实现线程模型.Linux是一种“多进程单线程”的操作系统,Linux本身只有进程的概念,而其所谓的“线程 ...

  5. Go学习笔记(一):Ubuntu 环境下Go的安装

    本文是根据<Go Web 编程>,逐步学习 Ubuntu 环境下go的安装的笔记. <Go Web 编程>的URL地址如下: https://github.com/astaxi ...

  6. Ubuntu环境下SSH的安装及使用

    Ubuntu环境下SSH的安装及使用 SSH是指Secure Shell,是一种安全的传输协议,Ubuntu客户端可以通过SSH访问远程服务器 .SSH的简介和工作机制可参看上篇文章SSH简介及工作机 ...

  7. Ubuntu环境下的Redis 配置与C++使用入门

      Redis是一个高性能的key-value数据库. Redisedis的出现,非常大程度补偿了memcached这类key/value存储的不足,在部分场合能够对关系数据库起到非常好的补充作用.它 ...

  8. ubuntu 环境下的QT程序打包

    很多的时候 需要将自己写的QT 程序发布一下  所以今天教一下 怎么在ubuntu 环境下将自己的写的Qt 程序打包打包是为了不依赖 开发环境 和开发的库. 1. QtCreate使用Release版 ...

  9. ubuntu环境下eclipse的安装以及hadoop插件的配置

    ubuntu环境下eclipse的安装以及hadoop插件的配置 一.eclipse的安装 在ubuntu桌面模式下,点击任务栏中的ubuntu软件中心,在搜索栏搜索eclipse 注意:安装过程需要 ...

随机推荐

  1. 递归一个List<T>,可自己根据需要改造为通用型。

    /// <summary> /// 递归一个List<ProvinceOrg> /// </summary> /// <returns></ret ...

  2. 全新定义!免费开源ERP平台如何玩转工业互联网

    简述 IoT Box通过Wifi.蓝牙.USB.网线等方式连接设备.IoT再通过互联网连接到Odoo服务器 Odoo的各种应用通过IoT操作各种设备.例如,PoS应用通过IoT操作小票打印机.银行刷卡 ...

  3. Doskey命令详解

    转自:https://blog.csdn.net/u012993732/article/details/48626921 调用 Doskey.exe,它撤回 Windows XP 命令.编辑命令行并创 ...

  4. IconFont的iOS使用

    IconFont的使用 Iconfont-国内功能很强大且图标内容很丰富的矢量图标库,提供矢量图标下载.在线存储.格式转换等功能.阿里巴巴体验团队倾力打造,设计和前端开发的便捷工具. https:// ...

  5. 【English】20190428

    It is of paramount importance that极为重要的一点[pærəmaʊnt] strategizing around SOA围绕soa制定战略  efficient gov ...

  6. 一份.NET 容器化的调查小结

    小编在上个月在微信公众号"dotnet跨平台" 做了一个针对.NET 容器化的调查:https://mp.weixin.qq.com/s/oszbuIORT0G8XLLgMZzkn ...

  7. 【死磕 Spring】----- IOC 之 获取验证模型

    原文出自:http://cmsblogs.com 在上篇博客[死磕Spring]----- IOC 之 加载 Bean 中提到,在核心逻辑方法 doLoadBeanDefinitions()中主要是做 ...

  8. building tool

    Build Tool的含义是什么? building tool中文名称叫构建工具,BuildTools.jar是我们构建Bukkit,CraftBukkit,Spigot和Spigot-API的解决方 ...

  9. Linux命令大全汇总,让你工作更有效率!

    基础命令 useradd:用户名 -m:创建新用户 passwd 用户名:为用户设置密码(当创建新用户后,就需要设置密码) whoami:查看当前用户名 exit :退出当前用户,返回之前切换过来的用 ...

  10. 分享一些 Kafka 消费数据的小经验

    前言 之前写过一篇<从源码分析如何优雅的使用 Kafka 生产者> ,有生产者自然也就有消费者. 建议对 Kakfa 还比较陌生的朋友可以先看看. 就我的使用经验来说,大部分情况都是处于数 ...