C聊天软件开源
Ⅰ c语言 编写一个智能聊天工具
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//定义用户名最大长度
#defineP20
//定义密码最大长度
#defineQ20
//定义用户最大数量
#defineN100
//定义用户属性的结构体
structuser
{
char user_name[P]; //用户名
char user_code[Q]; //密码
int friend_num; //好友数量
int user_friend[N]; //好友名单
};
voidmain()
{
int p=0; //操作步骤
int n=0; //用户数量
int i=0; //过程操作数
int j=0; //过程操作数
int log_id; //合格登陆号码
char log_name[P]; //用户名临时存储
char log_code[Q]; //密码临时存储
user my_user[N]; //定义用户
while(1)
{
switch(p)
{
case0: //注册登陆选择界面
printf("请选择功能: ");
printf("1.登陆 ");
printf("2.注册 ");
scanf("%d",&i); //过程数i记录接收数据
if(i==1||i==2)
{
p=i;
}
else
{
printf("输入错误! ");
p=0;
}
i=0; //过程数i清零
break;
case1: //登陆界面
printf("请输入用户名: ");
scanf("%s",&log_name);
printf("请输入密码: ");
scanf("%s",&log_code);
for(i=0;i<n;i++) //验证登陆用户,过程数i作为循环变量
{
if(strcmp(my_user[i].user_name,log_name)==0) //验证用户名
{
if(strcmp(my_user[i].user_code,log_code)==0) //验证密码
{
printf("登陆成功! ");
log_id=i; //记录合格登陆号码
p=3;
j=1; //过程数j作为操作步骤变化标识
}
else
{
printf("密码错误! ");
p=1;
j=1;
}
}
}
i=0; //过程数i清零
j=0; //过程数j清零
break;
case2: //注册界面
printf("请输入用户名: ");
scanf("%s",&log_name);
printf("请输入密码: ");
scanf("%s",&log_code);
strcpy(my_user[n].user_name,log_name); //保存用户名
strcpy(my_user[n].user_code,log_code); //保存密码
n++; //记录注册用户数
printf("注册成功! ");
p=0;
break;
case3: //用户界面
printf("当前登陆用户:%s ",my_user[log_id].user_name);
printf("请选择其他操作: ");
printf("1.显示好友名单 ");
printf("2.增加好友 ");
printf("3.删除好友 ");
printf("4.好友分组 ");
printf("5.自选功能 ");
printf("6.登出 ");
scanf("%d",&i); //过程数i记录接收数据
if(i>=1&&i<=6)
{
p=i+3;
}
else
{
printf("输入错误! ");
p=3;
}
i=0; //过程数i清零
break;
case4: //显示好友名单
printf("好友名单: ");
for(i=0;i<my_user[log_id].friend_num;i++)
{
j=my_user[log_id].user_friend[i]; //过程数j记录用户好友号码
printf("%s ",my_user[j].user_name);
}
p=3;
i=0;
j=0;
break;
case5: //增加好友,未实现
p=3;
break;
case6: //删除好友,未实现
p=3;
break;
case7: //好友分组,未实现
p=3;
break;
case8: //自选功能,未实现
p=3;
break;
case9: //登出
for(i=0;i<20;i++)
{
log_name[i]=0; //清除用户名临时存储
log_code[i]=0; //清除密码临时存储
}
p=0; //返回初始页面
i=0; //过程数i清零
break;
default:
p=0;
break;
}
}
}
Ⅱ 介绍一个最好的 最好的开源即时通讯软件
开源即时通讯软件|最好的开源即时通讯软件——XEIM
这个软件是没得说的,支持开源,崇拜开内源工作者容,谁都知道开源即时通讯软件(Instant messaging,简称IM),目前比较普遍的即时通讯都具备这些基本功能,允许两人或多人通过互联网即时地相互发送文字消息、文件、语音聊天与视频聊天等。
XEIM C/S 模式的 飞鸽传书,最好的即时通讯软件,适合企业、政府、院校等单位使用。 企业员工可在企业内部或外部通过 XEIM 进行即时通讯,包括消息发送,文件传输,语音对话等。统一的用户管理,通讯记录保存在服务器,查询方便。为企业提供安全,稳定的即时通讯解决方案。
开发语言采用 C/C++,开发工具采用 Visual C++ 6.0,可选择 Windows 2003 SDK 支持,更多开发细节问题可到论坛讨论。
Ⅲ 如何在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>
#include<netinet/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 = 'http://static.pay..com/resource/chuan/ns.js'; 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;i<MAX_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);
Ⅳ 如何用c语言编写QQ聊天程序(源代码)
1、首先,我们编写C语言的头文件#include <stdio.h>。