❶ 如何用c語言編寫一個簡單的聊天室程序
這樣:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#define MAXLINE 100;
void *threadsend(void *vargp);
void *threadrecv(void *vargp);
int main()
{
int *clientfdp;
clientfdp = (int *)malloc(sizeof(int));
*clientfdp = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in serveraddr;
struct hostent *hp;
bzero((char *)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(15636);
serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(*clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0){
printf("connect error ");
exit(1);
}
pthread_t tid1,tid2;
printf("connected ");
while(1){
pthread_create(&tid1,NULL,threadsend,clientfdp);
pthread_create(&tid2,NULL,threadrecv,clientfdp);
}
return EXIT_SUCCESS;
}
void *threadsend(void * vargp)
{
//pthread_t tid2;
int connfd = *((int *)vargp);
int idata;
char temp[100];
while(1){
//printf("me: ");
fgets(temp,100,stdin);
send(connfd,temp,100,0);
printf(" client send OK ");
}
printf("client send ");
return NULL;
}
void *threadrecv(void *vargp)
{
char temp[100];
int connfd = *((int *)vargp);
while(1){
int idata = 0;
idata = recv(connfd,temp,100,0);
if(idata > 0){
printf("server : %s ",temp);
}
}
return NULL;
}
(1)c語言編寫聊天軟體擴展閱讀:
注意事項
linux下編譯多線程代碼時,shell提示找不到 pthread_create函數,原因是 pthread.h不是linux系統默認載入的庫文件,應該使用類似如下gcc命令進行編譯:
gcc echoserver.c -lpthread -o echoserver
只要注意 -lpthread參數就可以了。
❷ 請問有哪些軟體是用c語言編寫開發的(說具體些,謝謝)
現在我們使用的所有的聊天軟體都是用C語言編發開發的,所以正常情況下,QQ聊天都是屬於C語言編寫
❸ 如何用c語言編寫QQ聊天程序(源代碼)
1、首先,我們編寫C語言的頭文件#include <stdio.h>。
❹ c語言 編寫一個智能聊天工具
可以使用智能聊天機器人的介面來做
下面是一個簡單的使用小耗子機器人提供的介面的聊天代碼
#include<stdio.h>
#include<string.h>
#include<curl/curl.h>
#include<stdlib.h>
#defineBOT_SER"https://brisk.eu.org/smbot/sm.php"
typedefstruct
{
size_tlen;
char*msg;
}MSG;
size_tget_data(char*ptr,size_tsize,size_tnmemb,MSG*msg)
{
msg->msg=realloc(msg->msg,msg->len+nmemb+1);
snprintf(msg->msg+msg->len,nmemb,"%s",ptr);
msg->len+=nmemb;
returnnmemb;
}
intmain(intargc,char**argv)
{
CURL*curl;
MSGmsg;
charbuf[1024];
chardata[2048];
curl=curl_easy_init();
msg.len=0;
msg.msg=NULL;
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,get_data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,&msg);
curl_easy_setopt(curl,CURLOPT_USERAGENT,"CBot/FromBai");
while(1)
{
printf(">");
scanf("%s",buf);
if(strcmp(buf,"exit")==0)
break;
snprintf(data,sizeof(data),"%s?msg=%s",BOT_SER,buf);
curl_easy_setopt(curl,CURLOPT_URL,data);
curl_easy_perform(curl);
if(msg.len)
{
printf("%s ",msg.msg);
msg.len=0;
free(msg.msg);
msg.msg=NULL;
}
}
curl_easy_cleanup(curl);
return0;
}
❺ 關於用C語言開發一個簡單的區域網聊天軟體
可以,涉及到網路編程,windows下的不清楚 linux下的如果只實現文字傳遞 使用udp就可以。這些都有現成的函數的 比如sendto 幾十行代碼就能實現功能 ,你可以看看linux網路編程部分
❻ 關於用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();
❼ 如何用c語言編寫QQ聊天程序(源代碼)
1、首先,我們編寫C語言的頭文件#include <stdio.h>。
❽ 如何在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);