如何用C语言编写一个简单的聊天室程序

如何用C语言编写一个简单的聊天室程序,第1张

这样:

#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\n")

      exit(1)

}

pthread_t tid1,tid2

printf("connected\n")

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: \n ")

fgets(temp,100,stdin)

send(connfd,temp,100,0)

printf("   友链       client send OK\n")

}

printf("client send\n")

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 :\n%s\n",temp)

}

}

return NULL

}

扩展资料:

注意事项

linux下编译多线程代码时,shell提示找键告蠢不到 pthread_create函数,原因是 pthread.h不是linux系统默认加载的库文件,应该使用类似如下gcc命令进行编译稿陪:

gcc echoserver.c -lpthread -o echoserver

只要注意 -lpthread参数就可以了。

如果想开发一个社交直播APP,其实除了普通的社交功能,个人设置中心的主要功能之外,更要做好服务器的搭建

直播源码平台搭建步骤:

1.、搭建框架:搭建直播平台时应先创建应用框架,一般我们选择UniApp,UniApp简单而且上手快,直播系统app源码使用它开发前端后,基本上不再需要其它的框架。app框架搭建:在APICloud Studio中直接创建应用上架,有三个常用页面框架备选。

2. 编码:整理好模块后,我们选用Java来猜州编辑页面和模块之间的调用,选用H5 +CSS3来APP的UI界面,实现App的编码过程。

3. 测试:通过真机穗运蔽和模拟进行各项功能悄悔模块的测试工作,分别用ios和安卓两个 *** 作系统进行测试,完善功能的优化及调整。

4. 交付上线:上传app的icon、启动页和证书,可生成iOS和Android 的原生安装包。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8239791.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存