C语言 linux环境基于socket的简易即时通信程序
转载请注明出处:http://www.cnblogs.com/kevince/p/3891033.html ——By Kevince
最近在看linux网络编程相关,现学现卖,就写了一个简易的C/S即时通信程序,代码如下:
head.h
/*头文件,client和server编译时都需要使用*/
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h> #define MSGLEN 1000
#define IPLEN 15 typedef int SOCKET;
server.c:
/*server*/ #include "head.h" char msg_recv[MSGLEN], msg_send[MSGLEN];
SOCKET server_sockfd, client_sockfd; void *thread_function(void *argv) /*线程函数*/
{
while(){
gets(msg_send);
write(client_sockfd, msg_send, MSGLEN);
}
pthread_exit(NULL);
} int main(int arg, char *argv[])
{
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
int port;
int res; pthread_t a_thread;
void *thread_result; if (arg != ){
printf("server --portnum\n");
exit(EXIT_FAILURE);
} sscanf(argv[], "%d", &port); /*读入端口*/ server_sockfd = socket(AF_INET, SOCK_STREAM, );
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
server_address.sin_port = htons(port); server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len); /*绑定端口并监听*/
listen(server_sockfd, );
printf("listen...\n"); client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
printf("connection success!\n"); res = pthread_create(&a_thread, NULL, thread_function, NULL); /*启动线程函数*/
if (res != ){
perror("Thread creation failed");
exit(EXIT_FAILURE);
} while(read(client_sockfd, msg_recv, MSGLEN)){
printf("msg from client: %s\n", msg_recv);
}
close(client_sockfd);
exit(EXIT_SUCCESS);
}
client.c:
/*client*/ #include "head.h" char msg_recv[MSGLEN],msg_send[MSGLEN];
SOCKET sockfd; void *thread_function(void *argv) /*线程函数*/
{
while(){
gets(msg_send);
write(sockfd, msg_send, MSGLEN);
}
pthread_exit(NULL);
} int main(int arg, char *argv[])
{
struct sockaddr_in address;
int len;
int res;
int port;
char ip[IPLEN]; void *thread_result;
pthread_t a_thread; sockfd = socket(AF_INET, SOCK_STREAM, ); if (arg != ){
printf("client --ipaddress --portnum\n");
exit(EXIT_FAILURE);
} sscanf(argv[], "%s", ip);
sscanf(argv[], "%d", &port); /*读取ip与端口*/ address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(ip);
address.sin_port = htons(port); len = sizeof(address);
res = connect(sockfd, (struct sockaddr *)&address, len);
if (res == -){
perror("connect failed! ");
exit(EXIT_FAILURE);
}
printf("connection success!\n"); res = pthread_create(&a_thread, NULL, thread_function, NULL); /*启动线程函数*/
if (res != ){
perror("Thread creation failed");
exit(EXIT_FAILURE);
} while(read(sockfd, msg_recv, MSGLEN)){
printf("msg from server: %s\n", msg_recv);
}
res = pthread_join(a_thread, &thread_result);
if (res != ){
perror("joined failed");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
由于使用了线程,所以要链接正确的线程库,所以编译命令如下:
gcc -D_REENTRANT -I/usr/include/nptl server.c -o server -L/usr/lib/nptl -lpthread gcc -D_REENTRANT -I/usr/include/nptl client.c -o client -L/usr/lib/nptl -lpthread
如果你的系统默认使用的就是NPTL线程库,那么编译时就无需加上-I和-L选项
运行时输入的命令规则是:
./server --portnum #即server后面要加上需要绑定的端口号。 ./client --ip --portnum #即client后面要加上服务器的IP地址以及端口号。
不积跬步无以至千里,虽然这两个程序很简单,但作为我的第一个linux环境下基于socket的通信程序,也很有纪念意义。
C语言 linux环境基于socket的简易即时通信程序的更多相关文章
- 第十三章:基于socket.io实现即时通信
安装好环境,请参考ionic环境搭建之windows篇 和 ionic环境搭建之OS X篇 . 服务器端的搭建参考socket io官网,里面有非常详细的描述,按照步骤下来,最终可以在localhos ...
- AIR32F103(三) Linux环境基于标准外设库的项目模板
目录 AIR32F103(一) 合宙AIR32F103CBT6开发板上手报告 AIR32F103(二) Linux环境和LibOpenCM3项目模板 AIR32F103(三) Linux环境基于标准外 ...
- 基于Android 平台简易即时通讯的研究与设计[转]
摘要:论文简单介绍Android 平台的特性,主要阐述了基于Android 平台简易即时通讯(IM)的作用和功能以及实现方法.(复杂的通讯如引入视频音频等可以考虑AnyChat SDK~)关键词:An ...
- 【ARM-Linux开发】Linux环境下使用eclipse开发C++动态链接库程序
Linux环境下使用eclipse开发C++动态链接库程序 Linux中也有类似windows中DLL的变成方法,只不过名称不同而已.在Linux中,动态链接叫做Standard Object,生成的 ...
- Python基于socket模块实现UDP通信功能示例
Python基于socket模块实现UDP通信功能示例 本文实例讲述了Python基于socket模块实现UDP通信功能.分享给大家供大家参考,具体如下: 一 代码 1.接收端 import ...
- linux环境基于python语言docx转pdf
windows平台因借助win32com具有多种方法将word转为pdf,但linux环境不具备此环境,win32com包也将import失败,那该如何做呢? # -*- coding: utf-8 ...
- Linux环境下编译并执行ava helloworld程序
原文:http://blog.lupaworld.com/home-space-uid-24466-do-blog-id-2578.html 已经学会怎样在Windows下怎样编辑,编译和运行Java ...
- go语言linux环境配置
linux的设置方法:有4个环境变量需要设置:GOROOT.GOPATH.GOBIN以及PATH.需要设置到某一个profile文件中(~/.bash_profile(单一用户)或/etc/profi ...
- Linux环境基于CentOS7 搭建部署Docker容器
1.Docker容器概述 区分Docker容器技术和VM虚拟机技术: evernotecid://394EFE90-9CE0-4D65-A8CD-DFEC0DC8061E/appyinxiangcom ...
随机推荐
- OpenCV2.4.9+VS2012安装与配置
需要下载并安装Visual Studio 2012 然后在OpenCV官网下载安装OpenCV2.4.9 for Windows,网址为http://opencv.org/downloads.html ...
- PCL学习笔记(一)
由于项目需要,开始学习一下HP公司的PCL打印语言,发现这方面的中文资料非常少,我做下记录也为后人提供便利. 关于PCL的介绍可以参考wiki百科 http://zh.wikipedia.org/zh ...
- @Html.ActionLink 添加样式 html标签
@Html.ActionLink(item.MessageTitle, "Detail", "News",new { MessageId = item.Mess ...
- MySQL----information-schema数据库相关权限的说明。
MySQL中的information_schema数据库比较特别有如下几个要注意的地方. 1.就算是一个新创建的用户,也就是说这个用户只有一个usage权限.它都可以查看informatoin_sch ...
- Oracle EBS-SQL (PO-9):检查期间采购订单执行情况.sql
--采购订单执行情况查询(七天内接收情况)select pha.segment1 采购订单, msib.segment1 物料编码, pla.qu ...
- Example: Develop Web application on Baidu App Engine using CherryPy
In the past few months, I have developed two simple applications on Baidu App Engine. Compared to Go ...
- 【Itext】7步制作Itext5页眉页脚pdf实现第几页共几页
itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...
- jdk7和8的一些新特性介绍
jdk7和8的一些新特性介绍 本文是我学习了解了jdk7和jdk8的一些新特性的一些资料,有兴趣的大家可以浏览下下面的内容. 官方文档:http://www.oracle.com/technetwor ...
- Spring、XML配置AOP
新建一个AOP类: public class MyInterceptor2 { public void doAccessCheck(){ System.out.println("前置通知 & ...
- Spring、Hello Spring
1.引入Spring jar包 2.新建一个Person 接口和Person Bean public interface PersonIService { public void helloSprin ...