當前位置:首頁 » 雲伺服器 » linux搭建聊天伺服器

linux搭建聊天伺服器

發布時間: 2023-11-11 01:26:39

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;
}

❷ 如何編寫linux聊天室

自從開始學linux網路編程後就想寫個聊天室,一開始原本打算用多進程的方式來寫,可是發覺進程間的通信有點麻煩,而且開銷也大,後來想用多線程能不能實現呢,於是便去看了一下linux里線程的用法,實際上只需要知道 pthread_create 就差不多了,於是動手開干,用了兩天時間,調試的過程挺痛苦的,一開始打算用純C來擼,便用簡單的數組來存儲客戶端的連接信息,可是運行時出現了一些很奇怪的問題,不知道是不是訪問了臨界資源,和線程間的互斥有關等等;奇怪的是,當改用STL的set或map時問題就解決了,但上網搜了下發現STL也不是線程安全的,至於到底是什麼問題暫時不想去糾結了,可能是其它一些小細節的錯誤吧。先貼上代碼:

首先是必要的頭文件 header.h:

#!/usr/bin/env python#-*- coding: utf-8 -*-from PyQt4 import QtGui, QtCoreimport sysimport socketimport threadclass Client(QtGui.QWidget):

BUF_LEN = 1024 def __init__(self, parent=None):

QtGui.QWidget.__init__(self, parent)

self.setWindowTitle(u'TCP客戶端')
self.resize(600, 500)
self.center()
layout = QtGui.QGridLayout(self)

label_ip = QtGui.QLabel(u'遠程主機IP:')
layout.addWidget(label_ip, 0, 0, 1, 1)
self.txt_ip = QtGui.QLineEdit('127.0.0.1')
layout.addWidget(self.txt_ip, 0, 1, 1, 3)

label_port = QtGui.QLabel(u'埠:')
layout.addWidget(label_port, 0, 4, 1, 1)
self.txt_port = QtGui.QLineEdit('9003')
layout.addWidget(self.txt_port, 0, 5, 1, 3)

self.isConnected = False
self.btn_connect = QtGui.QPushButton(u'連接')
self.connect(self.btn_connect, QtCore.SIGNAL( 'clicked()'), self.myConnect)
layout.addWidget(self.btn_connect, 0, 8, 1, 2)

label_recvMessage = QtGui.QLabel(u'消息內容:')
layout.addWidget(label_recvMessage, 1, 0, 1, 1)

self.btn_clearRecvMessage = QtGui.QPushButton(u'↓ 清空消息框')
self.connect(self.btn_clearRecvMessage, QtCore.SIGNAL( 'clicked()'), self.myClearRecvMessage)
layout.addWidget(self.btn_clearRecvMessage, 1, 7, 1, 3)

self.txt_recvMessage = QtGui.QTextEdit()
self.txt_recvMessage.setReadOnly(True)
self.txt_recvMessage.setStyleSheet('background-color:yellow')
layout.addWidget(self.txt_recvMessage, 2, 0, 1, 10)

lable_name = QtGui.QLabel(u'姓名(ID):')
layout.addWidget(lable_name, 3, 0, 1, 1)
self.txt_name = QtGui.QLineEdit()
layout.addWidget(self.txt_name, 3, 1, 1, 3)

self.isSendName = QtGui.QRadioButton(u'發送姓名')
self.isSendName.setChecked(False)
layout.addWidget(self.isSendName, 3, 4, 1, 1)

label_sendMessage = QtGui.QLabel(u' 輸入框:')
layout.addWidget(label_sendMessage, 4, 0, 1, 1)
self.txt_sendMessage = QtGui.QLineEdit()
self.txt_sendMessage.setStyleSheet("background-color:cyan")
layout.addWidget(self.txt_sendMessage, 4, 1, 1, 7)
self.btn_send = QtGui.QPushButton(u'發送')
self.connect(self.btn_send, QtCore.SIGNAL('clicked()'), self.mySend)
layout.addWidget(self.btn_send, 4, 8, 1, 2)

self.btn_clearSendMessage = QtGui.QPushButton(u'↑ 清空輸入框')
self.connect(self.btn_clearSendMessage, QtCore.SIGNAL( 'clicked()'), self.myClearSendMessage)
layout.addWidget(self.btn_clearSendMessage, 5, 6, 1, 2)
self.btn_quit = QtGui.QPushButton(u'退出')
self.connect(self.btn_quit, QtCore.

熱點內容
落櫻小屋哪裡下載安卓 發布:2025-01-27 12:35:13 瀏覽:71
微信伺服器IP跳轉 發布:2025-01-27 12:26:54 瀏覽:73
oracle自動備份腳本linux 發布:2025-01-27 12:21:40 瀏覽:936
pop伺服器密碼怎麼填 發布:2025-01-27 12:20:02 瀏覽:968
oraclesqlnumber 發布:2025-01-27 12:04:22 瀏覽:849
如何看三才配置數理暗示力 發布:2025-01-27 12:04:15 瀏覽:811
我的世界離線2b2t的伺服器 發布:2025-01-27 11:51:25 瀏覽:144
網站被異常篡改訪問有風險 發布:2025-01-27 11:50:01 瀏覽:431
光遇國際服腳本全部圖 發布:2025-01-27 11:47:40 瀏覽:139
ios資源加密 發布:2025-01-27 11:36:33 瀏覽:816