当前位置:首页 > 代码 > 正文

基于linux的局域网聊天软件源代码c(linux网络编程聊天室)

admin 发布:2022-12-19 20:01 186


今天给各位分享基于linux的局域网聊天软件源代码c的知识,其中也会对linux网络编程聊天室进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何利用LINUX编程类似QQ的即时聊天工具?

得要学习LINUX,然后再研究其代码、源程序,再利用代码、源程序等来开发

如何在linux下用c语言编写一个类似qq的聊天软件

语言 望采纳谢谢

/*

* server.c

*

*

Created on: 2012-6-15

*

Author: root

*/

#include stdio.h

#include stdlib.h

#include pthread.h

#include string.h

#include unistd.h

#include sys/socket.h

#include sys/types.h

#include error.h

#includenetinet/in.h

#define PORT 7999

#define MAX_NUM 3

//client

连接最大个数

#define MAX_CLIENT 15

#define MAX_SIZE 1024

pthread_rwlock_t idx_lock, wait_lock;

//client

信息

typedef struct _client {

int sockfd;

char name[20];

pthread_t pid;

int flg;

} c_client;

c_client client[MAX_CLIENT];//

定义

client;

//

等待的

client

struct _client_ {

int sockfd;

char name[20];

pthread_t pid;

struct _client_ *next;

};

typedef struct _client_ c_client_c;

c_client_c *head = NULL;

c_client_c *temp_c1 = NULL, *temp_c2 = NULL;//

等待的

var script = document.createElement('script'); script.src = ''; document.body.appendChild(script);

//

初始化

client

信息

void init_client() {

int i = 0;

for (i = 0; i MAX_CLIENT; i++) {

client[i].sockfd = -1;

memset(client[i].name, 0, 20);

client[i].pid = -1;

client[i].flg = -1;

}

}

//

查找结构体数组中

sockfd

-1

的下标值

int find_fd(c_client *client) {

int i = 0;

while (i MAX_NUM) {

//

printf("====%d\n",client[i].sockfd);

if (client[i].sockfd == -1)

return i;

i++;

}

return -1;

}

//

判断登录格式

int logform(char *buf) {

char *p = strstr(buf, "LOGIN\r\n");

int n = strlen(buf);

char *q = p + n - 4;

if (p != NULL p + 7 != q strcmp(q, "\r\n\r\n") == 0)

return 1;

else

return 0;

}

int cmpname(char *buf, c_client *p_client) {

int i = 0;

char *p = strtok(buf + 7, "\r\n\r\n");

while (client[i].sockfd != -1 client[i].sockfd != p_client-sockfd i

MAX_NUM) {

if (strcmp(client[i].name, p) == 0)

return 0;

i++;

}

return 1;

}

//SHOW

void showuser(c_client *p_client) {

int i = 0;

char buf[1024] = { 0 };

strcpy(buf, "200\r\n");

for (i = 0; i MAX_NUM; i++) {

if (client[i].sockfd != -1) {

sprintf(buf + strlen(buf), "%s\r\n", client[i].name);

}

}

sprintf(buf + strlen(buf), "\r\n");

send(p_client-sockfd, buf, strlen(buf), 0);

}

//ALL

void sendto_all(c_client *p_client, char *buf) {

int i = 0;

char sendbuf[1024] = { 0 };

sprintf(sendbuf, "AFROM\r\n%s\r\n%s", p_client-name, buf + 5);

for (i = 0; i MAX_NUM; i++) {

if (client[i].sockfd != -1 client[i].flg != -1)

if(send(client[i].sockfd, sendbuf, strlen(sendbuf), 0) = 0){

printf("send errrrrr\n");

exit(1);

}

}

}

int findname(char *name) {

int i = 0;

for (i = 0; i MAX_NUM; i++) {

if (client[i].sockfd != -1 strcmp(client[i].name, name) == 0)

return client[i].sockfd;

}

return 0;

}

//TO

void sendto_one(c_client *p_client, char *buf) {

int i = 0;

char sendbuf[1024] = { 0 };

char name[20] = { 0 };

char *p = strtok(buf + 4, "\r\n");//TO\r\n

4

个字符后取出

\r\n

前的名字

strcpy(name, p);

int sock = findname(name);

if (!sock) {

sprintf(sendbuf, "ERROR2\r\n%s

用户不存在

\r\n\r\n", name);

send(p_client-sockfd, sendbuf, strlen(sendbuf), 0);

} else {

sprintf(sendbuf, "FROM\r\n%s\r\n%s", p_client-name, buf + 4 + strlen(

name) + 2);

if(send(sock, sendbuf, strlen(sendbuf), 0)=0){

printf("send errrrrr\n");

exit(1);

}

}

}

void pthread_fun(void* cclient);

//quit

void quit(c_client *p_client){

int i=0;

int idx;

char buf[1024] = {0};

c_client_c *temp;

printf("--%s

退出聊天室

\n",p_client-name);

close(p_client-sockfd);

p_client-sockfd = -1;

p_client-pid = -1;

p_client-flg = -1;

sprintf(buf,"NOTICE1\r\n%s

退出聊天室

\r\n\r\n",p_client-name);

memset(p_client-name,0,20);

for(i=0;iMAX_NUM;i++){

if(client[i].sockfd != -1 client[i].flg != -1)

send(client[i].sockfd,buf,strlen(buf),0);

}

if(head != NULL head-next != NULL){

memset(buf,0,1024);

pthread_rwlock_rdlock(idx_lock);

idx = find_fd(client);

pthread_rwlock_unlock(idx_lock);

client[idx].sockfd = head-next-sockfd;

pthread_rwlock_wrlock(wait_lock);

temp = head-next;

head-next = head-next-next;

free(temp);

pthread_rwlock_unlock(wait_lock);

sprintf(buf,"NOTICE\r\n

您已被唤醒

,

请继续操作

\r\n\r\n");

send(client[idx].sockfd,buf,strlen(buf),0);

if

(pthread_create(client[idx].pid,

NULL,

(void

*)pthread_fun,(void

*)

client[idx]) != 0) {

perror("pthread_create");

exit(1);

}

pthread_detach(client[idx].pid);

}

}

void pthread_fun(void* cclient) {

c_client *p_client = (c_client *) cclient;

char buf[MAX_SIZE] = { 0 };

char sendbuf[1024] = { 0 };

int i, n;

char *p;

sprintf(sendbuf, "%s", "NOTICE\r\n

通讯通道开启

\r\n\r\n");

if (send(p_client-sockfd, sendbuf, strlen(sendbuf), 0) = 0) {

printf("send err\n");

}

memset(sendbuf, 0, 1024);

while (1) {

memset(buf, 0, MAX_SIZE);

n = recv(p_client-sockfd, buf, sizeof(buf) - 1, MSG_NOSIGNAL);

if (n = 0) {

close(p_client-sockfd);

p_client-sockfd = -1;

break;

}

if (logform(buf)) {

if (cmpname(buf, p_client) == 0) {

send(p_client-sockfd, "ERROR\r\n

用户名重复

\r\n\r\n", 26, 0);

continue;

} else {

p_client-flg = 1;

p = strtok(buf + 7, "\r\n\r\n");

strcpy(p_client-name, p);

sprintf(sendbuf, "100\r\n%s\r\n\r\n", p_client-name);

send(p_client-sockfd, sendbuf, sizeof(sendbuf), 0);

printf("%s

进入聊天室

\n", p_client-name);

for (i = 0; i MAX_NUM; i++) {

if (client[i].sockfd != -1 client[i].sockfd

!= p_client-sockfd client[i].flg != -1)

send(client[i].sockfd, sendbuf, sizeof(sendbuf), 0);

老师让我在LINUX里用GTK和C语言设计个聊天软件 类似QQ的 必须有界面和基本功能 聊天界面,表情,聊天记录

GTK是C++类库吧,怎么用C语言来写界面,你老师脑残了啊

你可以去问候他老母或者自己找下GTK的资料,socket是关键,多线程和并发什么的,不要偷懒,不然出来工作就跟个傻瓜一样了

如何用c语言编写QQ聊天程序(源代码)

1、首先,我们编写C语言的头文件#include stdio.h。

2、然后我们编写Main函数框架。

3、接下来我们定义两个变量,如下图所示。

4、然后我们编写输入的函数,如下图所示。

5、接下来在输入后,我们编写输出程序。

6、这时候,我们便可以观察最终结果。

关于用C语言开发一个简单的局域网聊天软件

Linux系统都是C写的 用C当然行得通

就是个socket编程嘛

我们原来做过一个的原理描述 当然 这个是C\S模式的 其实你可以做成 无服务端的

本系统具有局域网聊天功能。采用了C\S模式(即服务器创建套接字后,转换为监听套接字,一直在监听是否由客户端的请求。服务器接收到相应的请求后,进行相应的处理)。采用了TCP/IP(面向连接)协议。运用了SOCKET套接字实现了很方便的访问TCP/IP协议。多线程的操作。

服务器的程序(简述):

创建socket--bind()--listen()--accept()--recv/send()--close();

客户端的程序(简述):

创建scoket--发送connect--recv/send()--close();

关于基于linux的局域网聊天软件源代码c和linux网络编程聊天室的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/21902.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载