linux即时聊天源码,最简单的Linux命令行Socket聊天程序源代码

linux即时聊天源码,最简单的Linux命令行Socket聊天程序源代码只有今天贴出代码,明天看才知道自己有多么傻。单线程,一对一聊天,混搭风格编程,函数乱入不解释……/**ChatonLinuxTerminal–alpha*WortebyJimmy’steam@uestc*2011-2-23**Thisisthesorcecodeofclient*SomeBUGSstillunsloved,butwearetryin…

大家好,又见面了,我是你们的朋友全栈君。

只有今天贴出代码,明天看才知道自己有多么傻。

单线程,一对一聊天,混搭风格编程,函数乱入不解释……

/*

* Chat on Linux Terminal–alpha

* Worte by Jimmy’s team@uestc

* 2011-2-23

*

* This is the sorce code of client

* Some BUGS still unsloved, but we are trying our best to debug

* Be sure that your system’s port “1234” is not busy!

*

* */

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define SERVPORT 1234

#define MAX_DATA_SIZE 1024

int main(int argc, char *argv[]) {

int sockfd, sendBytes,recvBytes;

char sendBuf[MAX_DATA_SIZE],recvBuf[MAX_DATA_SIZE];

struct hostent *host;

struct sockaddr_in servAddr;

if(argc != 2) {

fprintf(stderr,”usage:./client [hostname]”);

exit(1);

}

/*translate the address*/

if((host = gethostbyname(argv[1])) == NULL) {

perror(“fail to get host by name”);

exit(1);

}

printf(“Success to get host by name…\n”);

/*establish a socket*/

if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror(“fail to establish a socket”);

exit(1);

}

printf(“Success to establish a socket…\n”);

/*init sockaddr_in*/

servAddr.sin_family = AF_INET;

servAddr.sin_port = htons(SERVPORT);

servAddr.sin_addr = *((struct in_addr *)host -> h_addr);

bzero(&(servAddr.sin_zero), 8);

/*connect the socket*/

if(connect(sockfd, (struct sockaddr *)&servAddr, sizeof(struct sockaddr_in)) == -1) {

perror(“fail to connect the socket”);

exit(1);

}

printf(“Success to connect the socket…\n”);

printf(“\033[40;32mWelcome to join %s!\033[1m\n”, inet_ntoa(servAddr.sin_addr));//include color set

while(1) {

/*send datas to server*/

printf(“Client:”);

gets(sendBuf);

if((sendBytes = send(sockfd, sendBuf, strlen(sendBuf), 0)) != strlen(sendBuf)) {

perror(“fail to send datas”);

exit(1);

}

printf(“(Success to send data!)\n”);

memset(sendBuf, 0x00, MAX_DATA_SIZE);

/*receive datas from server*/

if((recvBytes = recv(sockfd, recvBuf, MAX_DATA_SIZE, 0)) == -1) {

perror(“fail to receive datas”);

exit(1);

}

printf(“Server: %s \n”, recvBuf);

memset(recvBuf, 0x00, MAX_DATA_SIZE);

}

close(sockfd);

}

/*

* Chat on Linux Terminal–alpha

* Worte by Jimmy’s team@uestc

* 2011-2-23

*

* This is the sorce code of server

* Some BUGS still unsloved, but we are trying our best to debug

* Be sure that your system’s port “1234” is not busy!

*

* */

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define SERVPORT 1234

#define BACKLOG 20

#define MAX_CON_NO 10

#define MAX_DATA_SIZE 1024

int main(int argc, char *argv[]) {

struct sockaddr_in serverSockaddr, clientSockaddr;

int sinSize, recvBytes, sendBytes;

fd_set readfd;

fd_set writefd;

int sockfd, clientfd;

char sendBuf[MAX_DATA_SIZE], recvBuf[MAX_DATA_SIZE];

if(argc != 1) {

printf(“usage:./server\n”);

exit(1);

}

/*establish a socket*/

if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror(“fail to establish a socket”);

exit(1);

}

printf(“Success to establish a socket…(sockfd = %d)\n”, sockfd);

/*init sockaddr_in*/

serverSockaddr.sin_family = AF_INET;

serverSockaddr.sin_port = htons(SERVPORT);

serverSockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

bzero(&(serverSockaddr.sin_zero), 8);

int on = 1;

setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

/*bind socket*/

if(bind(sockfd, (struct sockaddr *)&serverSockaddr, sizeof(struct sockaddr))== -1) {

perror(“fail to bind”);

exit(1);

}

printf(“Success to bind the socket…\n”);

/*listen on the socket*/

if(listen(sockfd, BACKLOG) == -1) {

perror(“fail to listen”);

exit(1);

}

printf(“Success to listen on the socket…\n”);

while(1) {

FD_ZERO(&readfd);

FD_SET(sockfd, &readfd);

sinSize = sizeof(struct sockaddr_in);

if(select(MAX_CON_NO, &readfd, NULL, NULL, (struct timeval *)0) > 0) {

if(FD_ISSET(sockfd, &readfd) > 0) {

/*accept a client’s request*/

if((clientfd = accept(sockfd, (struct sockaddr *)&clientSockaddr, &sinSize)) == -1) {

perror(“fail to accept”);

exit(1);

}

printf(“Success to accpet a connection request…\n”);

printf(“\033[40;32m%s join in!\033[1m\n”, inet_ntoa(clientSockaddr.sin_addr));//include color set

while(1) {

/*receive datas from client*/

if((recvBytes = recv(clientfd, recvBuf, MAX_DATA_SIZE, 0)) == -1) {

perror(“fail to receive datas”);

exit(1);

}

printf(“Client:%s\n”, recvBuf);

memset(recvBuf, 0x00, MAX_DATA_SIZE);

/*send datas to client*/

printf(“Server:”);

gets(sendBuf);

if((sendBytes = send(clientfd, sendBuf, strlen(sendBuf), 0)) != strlen(sendBuf)) {

perror(“fail to send datas”);

exit(1);

}

printf(“(Success to send data!)\n”);

memset(sendBuf, 0x00, MAX_DATA_SIZE);

}

}

close(sockfd);

}

}

}

运行方法:

jimmy@MyPet:~$ gcc -o client client.c

jimmy@MyPet:~$ gcc -o server server.c

jimmy@MyPet:~$ ./server

Success to establish a socket…(sockfd = 3)

Success to bind the socket…

Success to listen on the socket…

jimmy@MyPet:~$ ./client MyPet

Success to get host by name…

Success to establish a socket…

Success to connect the socket…

Welcome to join 127.0.1.1!

Client:

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146382.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 单片机spi通信_stm32单片机常用的片内外设

    单片机spi通信_stm32单片机常用的片内外设提示:若转载,请备注来源,谢谢!文章目录前言一、SPI协议是什么?1.优点2.缺点3.结构二、SPI协议1.模式概念理解2.通信过程分析3.SPI个人协议理解总结前言题目上写的是单片机,其实不管你的板子上不上系统(FreeRtos、Linux),协议都是不变的。题外话:工作过程中,一直在移植别人写好的SPI协议,然后和外设的芯片(例如:Flash芯片、NFC芯片等)进行通信,但是都没有往底层深入的看,下午看了照着代码看了三个多小时,写这篇博客作为总结。一、SPI协议是什么?S

    2022年10月9日
    3
  • Postman下载安装

    Postman下载安装一、Postman下载1、Postman64位安装包下载:链接:https://pan.baidu.com/s/1lInTrSfW9PMmYI6OsUS2tg提取码:nvbw2、Postman32位安装包下载:链接:https://pan.baidu.com/s/1S_7K9wqyDwuoe-mjQowpKQ提取码:sw7g3、官网下载:地址:https://www.getpostman.com/downloads/,选择页面中的“Download”,根据自己电脑配置,选择32位下载还是64

    2022年6月16日
    47
  • c语言sizeof()_sizeof函数的用法

    c语言sizeof()_sizeof函数的用法sizeof是C语言中保留关键字,也可以认为是一种运算符,单目运算符。常见的使用方式:inta=10;intarr=[1,2,3];charstr[]="hello";intlen_a=sizeof(a);intlen_arr=sizeof(arr);intlen_str=sizeof(str)printf("len_a=%d,len_arr=%d,le…

    2022年9月17日
    2
  • docker(3)快速搭建centos7-python3.6环境[通俗易懂]

    docker(3)快速搭建centos7-python3.6环境[通俗易懂]前言当我们在一台电脑上搭建了python3.6的环境,下次换台电脑,又得重新搭建一次,设置环境变量等操作。好不容易安装好,一会提示pip不是内部或外部命令,一会又提示pip:commandno

    2022年7月30日
    8
  • 微信小程序轮播图调用接口

    微信小程序轮播图调用接口生命周期函数,页面加载:onLoad:function(options){let_this=this;wx.request({url:’http://www.day.com/index.php/img’,//仅为示例,并非真实的接口地址method:”GET”,success(res){//console.log(res.data.data)letdatas=res.data.data;//渲染_this.setData({datas})},}

    2022年5月11日
    45
  • M/F(人均gdp突破一万美元)

    https://image.uc.cn/o/wemedia/s/upload/2019/25f12a866b249c70e449416b79f137e1.png;,4,png;3,700x.png最近BTC暴涨的行情相信让很多分析家打脸了,多少阻力位、压力位一分钟破一个,.过去3个月自己亲身经历眼看着比特币从3000-4000左右的价格慢慢爬升,到大概6000左右的时候,很多非常资深的二级市场投…

    2022年4月13日
    101

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号