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 ...
随机推荐
- mysql_config_editor程序的用法
1.mysql_config_editor程序的作用: 它只是用来在用户的家目录下生成一个.mylogin.cnf 里面保存有用于登录mysql-server端的password,host,user信 ...
- listview优化
http://www.2cto.com/kf/201108/99928.html 项目用到ListView,由于要用到ImageView,图片源不是在资源里面的,没法使用资源ID,因此无法直接使用Si ...
- why constrained regression and Regularized regression equivalent
problem 1: $\min_{\beta} ~f_\alpha(\beta):=\frac{1}{2}\Vert y-X\beta\Vert^2 +\alpha\Vert \beta\Vert$ ...
- Linux学习十七、正规表达式练习题
情境模拟题一:透过 grep 搜寻特殊字串,并配合数据流重导向来处理大量的文件搜寻问题. 目标:正确的使用正规表示法: 前提:需要了解数据流重导向,以及透过子命令 $(command) 来处理档名的搜 ...
- C# 如何利用反射来加载程序集,并调用程序集中有关类的方法【转】
假设在C盘根目录下有个Dog的Dll程序集文件,该程序集文件中包含类Dog 该类中有个狗叫几声的方法,如何通过反射来加载这个C:\Dog.dll,并且调用Dog类里面的Sound方法呢: public ...
- [Python]ConfigParser解析配置文件
近期发现非常多接口配置都硬编码在souce file中了,于是就看了下python怎么解析配置文件,重构下这一块. 这个应该是早就要作的... 配置文件: [mysqld] user = mysql ...
- 网站图片列表动态显示、根据屏幕宽度动态设置DIV的CSS样式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 利用Gson进行String和对象的转换
利用Gson进行String和对象的转换 /** * 从JsonStr中解析BUserBase * @param jsonStr * @return */ public static BUserBas ...
- 在 PL/SQL Developer 中执行SQL文件的方法
打开 command Window SQL> @'D:\My Documents\Downloads\bde_chk_cbo.sql'; 整个路径及文件两边要有单引号哦!
- ios 中的构造方法
构造方法 1.什么是构造方法? 初始化对象的方法. 默认情况下,在 OC 当中创建1个对象分为两部分(new 做的事): +alloc:分配内存空间 -init :初始化对象 2.构造方法的作用是? ...