⑴ 用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]='