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 ...
随机推荐
- 获取mysqli函数的值和字段名
<?php $mysqli=new mysqli("localhost", "root", "123456", "xsphp ...
- MFC常用 控制对话框透明属性函数
void CFloatWnd::OnUpdateTransparent(int iTransparent){ HINSTANCE hInst = LoadLibrary("User32.DL ...
- VB MSFlexGrid 用法
http://blog.itpub.net/15453304/viewspace-445608/ 问题一,MSFlexGrid 点击一行,显示背景颜色,然后得到行号 首先,右键单击Msflexgrid ...
- Linux 计算某文件夹下的所有文件的md5值
使用find 命令 find /root -type f -print0 |xargs -0 md5sum >a.md5 校验的话 md5sum -c a.md5
- 有关于/home出现100%被占用 与 vnc的关系
我在远程服务器时,通常使用的程序是vnc.因为vnc可以图形化界面,操作效果比用putty好很多. 但是,我发现使用vnc有一个问题,就是/home占用空间会达到100%. [zy@islab62 ~ ...
- Visual Studio 2012 Update3 安装失败错误“正在关闭管道'
问题描述: Visual Studio 2012 update3 安装失败错误“ 正在关闭管道' 环境: Windows 7 SP1(x86和x64) Windows 8(x86和x64) Windo ...
- CentOS Linux 语言环境设置
程序运行使用一套语言需要有字符集(数据)和字体(显示),Locale是根据计算机用户所使用的语言,所在国家或者地区,以及当地的文化传统所定义的一个软件运行时的语言环境. 一.locale详解 在 Li ...
- Spring、编码剖析Spring管理Bean的原理
引入dom4j jar包 1.新建Person接口和PersonBean public interface PersonIService { public void helloSpring(); } ...
- process有个env属性,env属性就是环境变量,里面可以访问到NODE_ENV;NODE_ENV是在启动nodejs时添加上去的;
添加命令 为export NODE_ENV=production:
- MGTemplateEngine 模版引擎简单使用(转)
原文:http://blog.csdn.net/crazy_srufboy/article/details/21748995 要实现的效果 首先上图中间的 标题至内容 都是使用UIWebView显示, ...