当前位置: 技术问答>linux和unix
求一个用SOCKET编写的聊天程序,给300分,不够还可以加!!很急!!!!
来源: 互联网 发布时间:2015-06-23
本文导语: 求一个用SOCKET编写的聊天程序 | 最近刚好在学习socket和多线程的知识(linux9.0下),自己写了个小程序,可以多个人同时跟server聊天,但是不同client之间聊天的功能还没加!不知道满不满足你...
求一个用SOCKET编写的聊天程序
|
最近刚好在学习socket和多线程的知识(linux9.0下),自己写了个小程序,可以多个人同时跟server聊天,但是不同client之间聊天的功能还没加!不知道满不满足你的要求:使用方法,server端:./serverTalker
client端 ./clientTalker serverIp
程序如下:
/* File: server.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "talker.h"
int main(int argc, char **argv)
{
int socket_des;
int read_len;
int address_len;
int real_send_len;
int real_receive_len;
char server_input[BUFFER_SIZE];
char client_message[BUFFER_SIZE];
struct timeval tv;
struct sockaddr_in address;
fd_set readfds;
tv.tv_sec = 20;
tv.tv_usec = 500000;
socket_des = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(PORT);
bzero(address.sin_zero,8);
address_len = sizeof(address);
bind(socket_des, (struct sockaddr *)&address, address_len);
listen(socket_des, 5);
system("clear");
while(1) {
int len;
pid_t pid;
int client_sock_des;
struct sockaddr_in client_address;
len = sizeof(client_address);
client_sock_des = accept(socket_des,(struct sockaddr *)&client_address, &len);
if( (pid = fork()) == 0){
close(socket_des);
printf("Get connection from client %sn",inet_ntoa(client_address.sin_addr));
real_send_len = send(client_sock_des,"welcome connect to men",strlen("welcome connect to men"),0);
while(1){
FD_ZERO(&readfds);
FD_SET(STDIN,&readfds);
FD_SET(client_sock_des,&readfds);
select(client_sock_des+1,&readfds,NULL,NULL,&tv);
if(FD_ISSET(STDIN,&readfds)){
//send to server
read_len = read(STDIN,server_input,sizeof(server_input));
server_input[read_len] = '';
real_send_len = send(client_sock_des,server_input,read_len,0);
//need some process here
printf("send to client::%s",server_input);
}else if(FD_ISSET(client_sock_des,&readfds)){
//receive and send to output
real_receive_len = recv(client_sock_des,client_message,BUFFER_SIZE,0);
if(real_receive_len==0){
exit(0);
}else{
client_message[real_receive_len] = '';
printf("from client::%s",client_message);
}
}else{
close(client_sock_des);
printf("time outn");
exit(1);
}
}
close(client_sock_des);
exit(0);
}
close(client_sock_des);
}
exit(0);
}
client端 ./clientTalker serverIp
程序如下:
/* File: server.c */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "talker.h"
int main(int argc, char **argv)
{
int socket_des;
int read_len;
int address_len;
int real_send_len;
int real_receive_len;
char server_input[BUFFER_SIZE];
char client_message[BUFFER_SIZE];
struct timeval tv;
struct sockaddr_in address;
fd_set readfds;
tv.tv_sec = 20;
tv.tv_usec = 500000;
socket_des = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(PORT);
bzero(address.sin_zero,8);
address_len = sizeof(address);
bind(socket_des, (struct sockaddr *)&address, address_len);
listen(socket_des, 5);
system("clear");
while(1) {
int len;
pid_t pid;
int client_sock_des;
struct sockaddr_in client_address;
len = sizeof(client_address);
client_sock_des = accept(socket_des,(struct sockaddr *)&client_address, &len);
if( (pid = fork()) == 0){
close(socket_des);
printf("Get connection from client %sn",inet_ntoa(client_address.sin_addr));
real_send_len = send(client_sock_des,"welcome connect to men",strlen("welcome connect to men"),0);
while(1){
FD_ZERO(&readfds);
FD_SET(STDIN,&readfds);
FD_SET(client_sock_des,&readfds);
select(client_sock_des+1,&readfds,NULL,NULL,&tv);
if(FD_ISSET(STDIN,&readfds)){
//send to server
read_len = read(STDIN,server_input,sizeof(server_input));
server_input[read_len] = '';
real_send_len = send(client_sock_des,server_input,read_len,0);
//need some process here
printf("send to client::%s",server_input);
}else if(FD_ISSET(client_sock_des,&readfds)){
//receive and send to output
real_receive_len = recv(client_sock_des,client_message,BUFFER_SIZE,0);
if(real_receive_len==0){
exit(0);
}else{
client_message[real_receive_len] = '';
printf("from client::%s",client_message);
}
}else{
close(client_sock_des);
printf("time outn");
exit(1);
}
}
close(client_sock_des);
exit(0);
}
close(client_sock_des);
}
exit(0);
}