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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • DB9 串口母对母转接头是个坑「建议收藏」

    DB9串口母对母转接头是个坑以前在使用Mini2440开发板时见有人使用「DB9母对母转换头」充当「USB转串口(公)」到「开发板串口(公)」,让杂乱的桌子上少了一根线,让我感到惊奇,原来可以这样,也兴冲冲地购买并使用之。后来的后来,工作不再是烧写烧写系统固件后,慢慢对各种协议标准进一步了解,包括硬件接线的了解。尽管就一个小小的DB9串口接头,公头就应该有一个公头的样子;母头就应该有一个母头的样子。

    2022年4月17日
    39
  • AWS EC2 增加多个弹性 IP[通俗易懂]

    AWS EC2 增加多个弹性 IP

    2022年2月13日
    47
  • clover默认引导mac(clover win10引导)

    搞定Clover引导的Win&Mac双系统系统迁移至SSD作者:毛毛卷日期:2018-07-20字体大小:小中大从发完贴到现在总算搞定了,具体操作记录如下:由于当年折腾双系统的时候就经历了很多波折而且一般是默认启动MAC而我却是WIN10所以本身的要求和实现方法就有点特殊因此最终并没有偷懒用分区克隆的方法还是按部就班的进行首先把自己提的几个问题回答一下吧首先大概试了A…

    2022年4月11日
    38
  • c语言findwindowex函数用法,VB中findwindowex函数的用法?

    c语言findwindowex函数用法,VB中findwindowex函数的用法?FindWindowEx函数函数功能:在窗口列表中寻找与指定条件相符的第一个子窗口。该函数获得一个窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数查找子窗口,从排在给定的子窗口后面的下一个子窗口开始。在查找时不区分大小写。参数:(1)hwndParent:要查找的子窗口所在的父窗口的句柄(如果设置了hwndParent,则表示从这个hwndParent指向的父窗口中搜索子窗口)。如…

    2022年5月31日
    35
  • android开发之GPS定位详解

    一、LocationManagerLocationMangager,位置管理器。要想操作定位相关设备,必须先定义个LocationManager。我们可以通过如下代码创建LocationManger对象。LocationManger locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE

    2022年3月10日
    39
  • 谓词表示法表示猴子摘香蕉_猴子妈妈有14个香蕉

    谓词表示法表示猴子摘香蕉_猴子妈妈有14个香蕉案例:我们要实现以下步骤:这个案例共有以下几种情况,猴子香蕉箱子在同一处,猴子香蕉在同一处,香蕉箱子在同一出,还有三者均不在同一处,但不论是哪种情况,我们需要清楚一点就算是香蕉和猴子在同一位置,猴子也无法直接获得香蕉,因此我们第一步必须需要先找到箱子,然后再去搬着箱子移动到香蕉处。本案例中有以下四个谓词逻辑: Run(monkey,box)代表猴子去搬箱子 Getbox(monkey,box)代表猴子得到了箱子 Run(monkey,banana)代表了.

    2022年9月26日
    2

发表回复

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

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