如何用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参数就可以了。

源码如下:

//chat_one.c

#include <sys/types.h>

#include <sys/socket.h>衫让团

#include <netinet/in.h>

#include <stdio.h>

#include <string.h>

#include <errno.h>

#include <malloc.h>或橘

#include <sys/select.h>

int main( int argc, char **argv)

{

struct sockaddr_in chatone, chattwo

char pmsg[1000]

char *buf

intchatone_fd

intret,i

intlen, msg_len

fd_set fdset

if ( argc <2 ){

printf("please input ip address\n")

return -1

}

printf("server address is %s\n", argv[1])

chattwo.sin_family = AF_INET

chattwo.sin_port = htons(60002)

inet_pton(AF_INET, argv[1], &chattwo.sin_addr.s_addr)

chatone.sin_family = AF_INET

chatone.sin_port = htons(60000)

chatone.sin_addr.s_addr = INADDR_ANY

chatone_fd = socket(PF_INET, SOCK_DGRAM, 0)

if ( -1 == chatone_fd ){

printf("create socket failed %s\n", strerror(errno))

return -1

}

ret = bind(chatone_fd, (struct sockaddr *)&chatone, sizeof(chatone))

if ( -1 == ret){

printf("bind failed %s \n", strerror(errno))

return -1

}

for(i=0i<1000i++){

FD_ZERO( &fdset )

FD_SET ( 0, &fdset)

FD_SET( chatone_fd, &fdset)

if ( -1 == select ( chatone_fd+1, &fdset, NULL, NULL, NULL) ){

continue

}

if ( FD_ISSET( chatone_fd, &fdset)){

recvfrom( chatone_fd, pmsg, 999, 0, NULL, 0)

printf("receive %s\n", pmsg)

}

else{

memset( pmsg, 0, 1000)

fgets(pmsg, 999, stdin)

len = sizeof(chattwo)

sendto( chatone_fd, pmsg, 1000, 0,\

(struct sockaddr*) &chattwo, len)

printf("send %s\n", pmsg)

}

}

printf("sent %d packets\n", i)

close(chatone_fd)

return 0

}

//chat_two.c

#include <sys/types.h>

#include <sys/滑蚂socket.h>

#include <netinet/in.h>

#include <stdio.h>

#include <string.h>

#include <errno.h>

#include <malloc.h>

#include <sys/select.h>

int main( int argc, char **argv)

{

struct sockaddr_in chatone, chattwo

char pmsg[1000]

char *buf

intchattwo_fd

intret,i

intlen, msg_len

fd_set fdset

if ( argc <2 ){

printf("please input ip address\n")

return -1

}

printf("server address is %s\n", argv[1])

chattwo.sin_family = AF_INET

chattwo.sin_port = htons(60002)

chattwo.sin_addr.s_addr = INADDR_ANY

chatone.sin_family = AF_INET

chatone.sin_port = htons(60000)

inet_pton(AF_INET, argv[1], &chatone.sin_addr.s_addr)

chattwo_fd = socket(PF_INET, SOCK_DGRAM, 0)

if ( -1 == chattwo_fd ){

printf("create socket failed %s\n", strerror(errno))

return -1

}

ret = bind(chattwo_fd, (struct sockaddr *)&chattwo, sizeof(chattwo))

if ( -1 == ret){

printf("bind failed %s \n", strerror(errno))

return -1

}

for(i=0i<1000i++){

FD_ZERO( &fdset )

FD_SET ( 0, &fdset)

FD_SET( chattwo_fd, &fdset)

if ( -1 == select ( chattwo_fd+1, &fdset, NULL, NULL, NULL) ){

continue

}

if ( FD_ISSET( chattwo_fd, &fdset)){

recvfrom( chattwo_fd, pmsg, 999, 0, NULL, 0)

printf("receive: %s\n", pmsg)

}

else{

memset( pmsg, 0, 1000)

fgets(pmsg, 999, stdin)

len = sizeof(chatone)

sendto( chattwo_fd, pmsg, 1000, 0,\

(struct sockaddr*) &chatone, len)

printf("send %s\n", pmsg)

}

}

printf("sent %d packets\n", i)

close(chattwo_fd)

return 0

}

编译好这个两个程序就可以进行简单的通信了。

这不是绝庆纤很简单,加个

在send前面加个

char password[20]="12345"

printf("please enter the password:")

scanf("%s",importpassword)

if(0==strcmp(password,importpassword))

{

连接。并仿差好。。send()

}

else 退出。或者这里还可以写个循环输入也可

如果需要用户名就麻烦多了。不多说了,求采纳,有问必答


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

原文地址: http://outofmemory.cn/yw/12562783.html

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

发表评论

登录后才能评论

评论列表(0条)

保存