当前位置:首页 » 编程语言 » c语言实现聊天程序
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言实现聊天程序

发布时间: 2023-06-11 05:35:44

⑴ 用c语言设计一对一的网络聊天的桌面应用程序

已经发送,第一次发错了,是之前写的程序,so sorry!

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

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<errno.h>

#include<sys/socket.h>

#include<arpa/inet.h>

#include<netinet/in.h>

#include<sys/types.h>

#include<unistd.h>

#include<sys/time.h>

#defineBUFLEN1024

#definePORT6666

#defineLISTNUM20

intmain()

{

intsockfd,newfd;

structsockaddr_ins_addr,c_addr;

charbuf[BUFLEN];

socklen_tlen;

unsignedintport,listnum;

fd_setrfds;

structtimevaltv;

intretval,maxfd;

/*建立socket*/

if((sockfd=socket(PF_INET,SOCK_STREAM,0))==-1){

perror("socket");

exit(errno);

}else

printf("socketcreatesuccess! ");

memset(&s_addr,0,sizeof(s_addr));

s_addr.sin_family=AF_INET;

s_addr.sin_port=htons(PORT);

s_addr.sin_addr.s_addr=htons(INADDR_ANY);

/*把地址和端口帮定到套接字上*/

if((bind(sockfd,(structsockaddr*)&s_addr,sizeof(structsockaddr)))==-1){

perror("bind");

exit(errno);

}else

printf("bindsuccess! ");

/*侦听本地端口*/

if(listen(sockfd,listnum)==-1){

perror("listen");

exit(errno);

}else

printf("theserverislistening! ");

while(1){

printf("*****************聊天开始*************** ");

len=sizeof(structsockaddr);

if((newfd=accept(sockfd,(structsockaddr*)&c_addr,&len))==-1){

perror("accept");

exit(errno);

}else

printf("正在与您聊天的客户端是:%s:%d ",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port));

while(1){

FD_ZERO(&rfds);

FD_SET(0,&rfds);

maxfd=0;

FD_SET(newfd,&rfds);

/*找出文件描述符集合中最大的文件描述符*/

if(maxfd<newfd)

maxfd=newfd;

/*设置超时时间*/

tv.tv_sec=6;

tv.tv_usec=0;

/*等待聊天*/

retval=select(maxfd+1,&rfds,NULL,NULL,&tv);

if(retval==-1){

printf("select出错,与该客户端连接的程序将退出 ");

break;

}elseif(retval==0){

printf("waiting... ");

continue;

}else{

/*用户输入信息了*/

if(FD_ISSET(0,&rfds)){

/******发送消息*******/

memset(buf,0,sizeof(buf));

/*fgets函数:从流中读取BUFLEN-1个字符*/

fgets(buf,BUFLEN,stdin);

/*打印发送的消息*/

//fputs(buf,stdout);

if(!strncasecmp(buf,"quit",4)){

printf("server请求终止聊天! ");

break;

}

len=send(newfd,buf,strlen(buf),0);

if(len>0)

printf(" 消息发送成功:%s ",buf);

else{

printf("消息发送失败! ");

break;

}

}

/*客户端发来了消息*/

if(FD_ISSET(newfd,&rfds)){

/******接收消息*******/

memset(buf,0,sizeof(buf));

/*fgets函数:从流中读取BUFLEN-1个字符*/

len=recv(newfd,buf,BUFLEN,0);

if(len>0)

printf("客户端发来的信息是:%s ",buf);

else{

if(len<0)

printf("接受消息失败! ");

else

printf("客户端退出了,聊天终止! ");

break;

}

}

}

}

/*关闭聊天的套接字*/

close(newfd);

/*是否退出服务器*/

printf("服务器是否退出程序:y->是;n->否?");

bzero(buf,BUFLEN);

fgets(buf,BUFLEN,stdin);

if(!strncasecmp(buf,"y",1)){

printf("server退出! ");

break;

}

}

/*关闭服务器的套接字*/

close(sockfd);

return0;

}

(2)c语言实现聊天程序扩展阅读

C语言编写一个简单的ATM系统

#include<stdio.h>

intchoice;

intshow_menu();

doubleget_money();

voiddeposit(double);

voidwithdraw(double);

doubleaccount=0.0;

intmain(intargc,constchar*argv[])

{

system("COLOR5f");

show_menu();

doubletemp;

while(choice!=0)

{

system("cls");

switch(choice)

{

case1:

printf("您的当前余额为:%.2f ",get_money());

break;

case2:

printf("请输入您的存款金额: ");

scanf("%lf",&temp);

deposit(temp);

printf("您的当前余额为:%.2f ",get_money());

break;

case3:

printf("您的当前余额为:%.2f ",get_money());

printf("请输入您的取款金额: ");

scanf("%lf",&temp);

withdraw(temp);

printf("您的当前余额为:%.2f ",get_money());

break;

default:

break;

}

getchar();

choice=show_menu();

}

printf("欢迎您下次再来中国银行为您提供的服务,再见! ");

return0;

}

intshow_menu()

{

printf("*****欢迎使用中国银行行为为您提供的服务****** ");

printf(" 1.查询账户余额2.存款3.取款0.退出 ");

printf(" 请选择服务种类:");

scanf("%d",&choice);

returnchoice;

}

doubleget_money()

{

returnaccount;

}

voiddeposit(doublemoney)

{

account=account+money;

}

voidwithdraw(doublemoney)

{

if(account<money)

printf("抱歉,余额不足,不能取%.2f这么多钱! ",money);

else

account=account-money;

}

⑶ linux下用c语言编写局域网聊天工具

/*服务器*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 5555
#define BACKLOG 10
#define MAX(a,b) ((a)>(b)?(a):(b))
void str_echo(int);
void server()
{
int sockfd,client_fd;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
socklen_t sin_size;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))== -1)
{
perror("socket create error!");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
perror("bind error!");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen error!");
exit(1);
}
while(1)
{
sin_size=sizeof(struct sockaddr_in);
if((client_fd=accept(sockfd,(struct sockaddr *)&remote_addr,&sin_size))==-1)
{
perror("accept error!");
continue;
}
printf("received a connection from %s\n",inet_ntoa(remote_addr.sin_addr));
if(!fork())
{
close(sockfd);
str_echo(client_fd);
exit(0);
}
close(client_fd);
}
}
void str_echo(int sockfd)
{
int maxfd;
char sendbuf[1024]={0};
char recvbuf[1024]={0};
fd_set rfds;

while(1)
{
FD_SET(0,&rfds);
FD_SET(sockfd,&rfds);
maxfd=MAX(0,sockfd)+1;

select(maxfd,&rfds,NULL,NULL,NULL);
if(FD_ISSET(sockfd,&rfds))
{
bzero(recvbuf,1024);
recv(sockfd,recvbuf,1024,0);
printf("C:%s\n",recvbuf);
}
if(FD_ISSET(0,&rfds))
{
read(0,sendbuf,1024);
send(sockfd,sendbuf,strlen(sendbuf),0);
bzero(sendbuf,1024);
}
}
}
int main()
{
server();
return 0;
}
/*客户端*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 5555
#define MAXSIZE 1024
#define MAX(a,b) ((a)>(b)?(a):(b))
void client(int argc,char **argv)
{
int sockfd,maxfd;
char sendbuf[MAXSIZE]={0};
char recvbuf[MAXSIZE]={0};
// struct hostent *host;
struct sockaddr_in serv_addr;
fd_set rfds;
/* if(argc<2)
{
fprintf(stderr,"Please enter the sserver's name!\n");
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL)
{
herror("get host by name error!\n");
exit(1);
}
*/
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket creat error!\n");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
// serv_addr.sin_addr=*((struct in_addr *)host->h_addr);
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
bzero(&(serv_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
{
perror("connection error!\n");
exit(1);
}
FD_ZERO(&rfds);
while(1)
{
FD_SET(0,&rfds);
FD_SET(sockfd,&rfds);
maxfd=MAX(0,sockfd)+1;
select(maxfd,&rfds,NULL,NULL,NULL);
if(FD_ISSET(sockfd,&rfds))
{
bzero(recvbuf,MAXSIZE);
recv(sockfd,recvbuf,MAXSIZE,0);
printf("S:%s\n",recvbuf);
}
if(FD_ISSET(0,&rfds))
{
fgets(sendbuf,MAXSIZE,stdin);
send(sockfd,sendbuf,strlen(sendbuf),0);
bzero(sendbuf,MAXSIZE);
}
}
// close(sockfd);
}
int main(int argc,char **argv)
{
client(argc,argv);
return 0;
}

⑷ 用C语言在WIN32下编译一个简单的聊天程序,客户端向服务器发消息,服务器接到消息后会返回一个

#include<stdio.h>#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<errno.h>
#include<netinet/in.h>
#include<sys/socket.h>

#define SERVERPORT 5555#define BACKLOG 10
#define MAXLEN 1024

int main(){
int sockfd,client_fd;
int sin_size = 0;
int recvlen = 0;
char recvbuf[MAXLEN]={0};
struct sockaddr_in my_addr;
struct sockaddr_in cli_addr;
int iRet;

/*建立socke通信*/
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
printf("Create socket error\n\n");
exit(1);
}

printf("socket success\n");

my_addr.sin_family=AF_INET; my_addr.sin_port=htons(SERVERPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
memset(my_addr.sin_zero,0,8);

/*绑定socket端口*/
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1){
printf("bind error\n");
close(sockfd);
exit(1);
}

printf("bind success\n");

/*监听端口*/
if(listen(sockfd,BACKLOG) == -1){
printf("listen error\n!");
close(sockfd);
exit(1);
}

printf("listren success\n");

while(1) {

sin_size = sizeof(struct sockaddr_in);

/*阻塞等待客户端发送消息*/
if((client_fd=accept(sockfd,(struct sockaddr *)&cli_addr,&sin_size)) == -1)
{
printf("accept error !");
continue;
}
printf("accept success\n");
printf("receive a connection form %s\n",inet_ntoa(cli_addr.sin_addr));

/*fork进程,子进程处理接收的信息*/
iRet=fork();
if(iRet < 0)
{
printf(" fork err, pid=[%d]", getpid());
}

/*fork返回0,为子进程*/
else if(iRet == 0)
{

printf("pid=[%d]", getpid());
printf("子进程\n");

memset(recvbuf,0,sizeof(recvbuf));

/*接收客户端发送的内容*/
if((recvlen = recv(client_fd,recvbuf,MAXLEN,0)) == -1)
{
printf("recv error\n");
close(client_fd);
exit(1);
}
printf("recvlen[%d]\n",recvlen);
printf("recvmsg:\n%s\n",recvbuf);

/*向客户端发送内容*/ if(send(client_fd,recvbuf,recvlen,0) == -1)
{
printf("send error\n");
close(client_fd);
exit(1);
}
printf("sendmsg:\n%s\n",recvbuf);

close(client_fd);
exit(0);

}

/*父进程,返回子进程pid*/
else
{
printf("pid=[%d]", getpid());
printf("父进程\n");
continue;
}

}

close(sockfd);
exit(0);
}

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

可以,涉及到网络编程,windows下的不清楚 linux下的如果只实现文字传递 使用udp就可以。这些都有现成的函数的 比如sendto 几十行代码就能实现功能 ,你可以看看linux网络编程部分

⑹ 求C语言高手,实现一个简单的TCPIP程序以实现两台计算机之间的聊天通信,

你上面给出的代码其实就是MSDN里面的演示代码,不过不完整,只演示了两个函数的使用,我给你看看我写的TCP通讯程序,可以在同一个局域网内的两台不同计算机之间聊天:

这其实就是某本将网络通讯的教程里面的例子,不过是我自己重写了一遍,下面给你代码:

========================

下面是公共代码:

========================

#ifndef__CINITSOCK__H__

#define__CINITSOCK__H__

#include<winsock2.h>

#include<iphlpapi.h>

#pragmacomment(lib,"ws2_32.lib")

#pragmacomment(lib,"iphlpapi.lib")

classCInitSock

{

public:

CInitSock(intnMinorVer=2,intnMajorVer=2)

{

WSADATAwsData;

WORDwVer=MAKEWORD(nMinorVer,nMajorVer);

if(0!=WSAStartup(wVer,&wsData))exit(0);

}

~CInitSock()

{

WSACleanup();

}

};

#endif

========================

下面是客户端的代码:

=======================

#include"CInitSock.h"

#include<iostream>

usingnamespacestd;

CInitSockg_Sock;

voidmain()

{

SOCKETsockClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

if(INVALID_SOCKET==sockClient)

return;

sockaddr_insockAddr;

sockAddr.sin_family=AF_INET;

sockAddr.sin_port=htons(4567);

sockAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");

if(-1==connect(sockClient,(sockaddr*)&sockAddr,sizeof(sockAddr)))

{

cout<<"connectfailed"<<endl;

return;

}

while(true)

{

charszBuf[MAX_PATH];

ZeroMemory(szBuf,sizeof(szBuf));

cout<<"YouSay:";

cin>>szBuf;

if(SOCKET_ERROR ==send(sockClient,szBuf,MAX_PATH,0))

{

cout<<"sendfailed"<<endl;

return;

}

intnRecvLen=recv(sockClient,szBuf,MAX_PATH,0);

if(nRecvLen>0)

{

//szBuf[nRecvLen]='';

cout<<"ServiceSay:"<<szBuf<<endl<<endl;

}

else

{

cout<<"recvfailed"<<endl;

return;

}

}

closesocket(sockClient);

}

======================

下面是服务端的代码:

======================

#include"CInitSock.h"

#include<iostream>

usingnamespacestd;

CInitSockg_Sock;

voidmain()

{

SOCKETsockClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

if(INVALID_SOCKET==sockClient)

return;

sockaddr_insockAddr;

sockAddr.sin_family=AF_INET;

sockAddr.sin_port=htons(4567);

sockAddr.sin_addr.S_un.S_addr=INADDR_ANY;

bind(sockClient,(sockaddr*)&sockAddr,sizeof(sockAddr));

listen(sockClient,SOMAXCONN);

charszBuf[MAX_PATH];

sockaddr_inremoteAddr;

intnLen=sizeof(sockaddr_in);

SOCKETsock=accept(sockClient,(sockaddr*)&remoteAddr,&nLen);

while(true)

{

intnRecvLen=recv(sock,szBuf,MAX_PATH,0);

if(nRecvLen>0)

{

cout<<"ClientSay:"<<szBuf<<endl<<endl;

}

ZeroMemory(szBuf,sizeof(szBuf));

cout<<"YouSay:";

cin>>szBuf;

if(SOCKET_ERROR==send(sock,szBuf,MAX_PATH,0))

{

cout<<"sendfailed"<<endl;

}

}

closesocket(sock);

closesocket(sockClient);

}

希望能够帮到楼主。

⑺ 如何用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;

}

(7)c语言实现聊天程序扩展阅读:

注意事项

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

gcc echoserver.c -lpthread -o echoserver

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

⑻ 求大神:编写点对点聊天程序 用C语言有代码

⑼ 用C语言写一个简单聊天软件!谢谢

听了楼上的我都不敢说Windows程序设计中的SDK用的也是c语言了

传的是《C语言高级编程及实例剖析》中的第六章的源码,用的是SDK编程,里面拉了控件和用了多线程

楼主如果需要界面比较复杂的聊天室程序,用MFC当然方便点,但用SDK也没有太大的问题(原理是一样的,学哪一种,另一种就会了)。后者的教程貌似很难找。可以看看MFC的,比较好的是 《Vc++ 打造局域网聊天室》(视频)

两种资料在网上都找得到...

补充:当然,如果需要传文件,图片那些,就需要研究研究网络协议了,也只是添加些功能而已