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

linux即时聊天源码,最简单的Linux命令行Socket聊天程序源代码只有今天贴出代码,明天看才知道自己有多么傻。单线程,一对一聊天,混搭风格编程,函数乱入不解释……/**ChatonLinuxTerminalalpha*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:

今天的文章linux即时聊天源码,最简单的Linux命令行Socket聊天程序源代码分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/6115.html

(0)
编程小号编程小号

相关推荐

发表回复

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