i want to create simple chat program.send message from client and the server forward this message to all clients who log entry.
this is server:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#include "myserver.h"
#define CLIENTS 10
#define PORT 20001
int check_ret;
static int id;
int server_close_socket(){
}
void * server_receive1 (void * par){
char buf[256];
int new_client = *((int *)par);
while(1){
bzero(buf,256);
recv(new_client,buf,256,0);
send(new_client,buf,256,0);
}
}
void * server_receive (void * par){
int new_client = *((int *)par);
id++;
char buf[256]="please,Enter the nickname";
char buf1[256]="New Client :";
char buf2[30]="failed to enter";
char *nickname[512];
pthread_t server_thread;
send(new_client,buf,sizeof(buf),0);
bzero(buf,256);
recv(new_client,buf,256,0);
strcat(buf1,buf);
printf("%s",buf1);int i;
pthread_create(&server_thread,NULL,server_receive1,&server_thread);
close(new_client);
}
void *server_connect(void * par){
int new_client;
pthread_t server_thread_receive;
int sockfd=*((int *)par);
struct sockaddr_in client_address;
int sockaddr_in_len = (sizeof(struct sockaddr_in));
while(1){
if ((new_client = accept(sockfd,(struct sockaddr *) &client_address,&sockaddr_in_len))
== -1) {
perror("accept");
continue;
}
else{
printf("accepte connect from %s:%d \n",inet_ntoa(client_address.sin_addr),ntohs
(client_address.sin_port));
pthread_create(&server_thread_receive,NULL,server_receive,&new_client);
}
}
}
int main(){
struct sockaddr_in server;
socklen_t sin_size=(sizeof(struct sockaddr_in));
pthread_t server_thread_connect;
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
printf("socket error. errno is %d\n", errno);
exit(1);
}
else{
printf("socket ok...\n");
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
bzero(&(server.sin_zero), 8);
check_ret = bind(sockfd,(struct sockaddr *) &server,sin_size);
if(check_ret < 0) {
printf("bind error. errno is %d\n", errno);
close(sockfd);
exit(1);
}
else{
printf("Bind ok...\n");
}
check_ret = listen(sockfd, 5);
if(check_ret < 0) {
printf("listen error. errno is %d\n", errno);
close(sockfd);
exit(1);
}
else{
printf("listen ok...\n waiting requestes...\n");
}
pthread_create(&server_thread_connect,NULL,server_connect,&sockfd);
while(1);
}
this is client:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include"myclient.h"
char str[256];
char str1[256];
void * client_send(void * par){
int sockfd = *((int *)par);
//memset(str,0,sizeof(str));
//fgets(str,256,stdin);
//send(sockfd,str,sizeof(str),0);
while(1){
sockfd = *((int *)par);
//memset(str,0,sizeof(str));
printf("\n>>");
fgets(str,256,stdin);
str[strlen(str)] = '\0';
send(sockfd,str,sizeof(str)+1,0);
memset(str,0,sizeof(str));
}
close(sockfd);
}
void * client_receive(void * par){
int sockfd = *((int *)par);
//memset(str,0,sizeof(str1));
//recv(sockfd,str1,sizeof(str1),0);
/*recv(sockfd,str1,sizeof(str1),0);
printf("\n%s",str1);
printf("\n");*/
while(1){
sockfd = *((int *)par);
recv(sockfd,str1,sizeof(str1),0);
printf("\n%s",str1);
fgets(str,256,stdin);
memset(str1,0,sizeof(str1));
}
close(sockfd);
}
int main(int argc, char *argv[]){
int sockfd;
struct hostent * host;
struct sockaddr_in client;
pthread_t client_thread_send;
pthread_t client_thread_receive;
if (argc != 3) {
printf ( "%s host port\n", argv[0] );
return -1;
}
if ((host = gethostbyname(argv[1])) == NULL) {
perror("gethostbyname");
exit(1);
}
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("socket");
return -1;
}
client.sin_family = AF_INET;
client.sin_port = htons(atoi(argv[2]));
memcpy( &client.sin_addr, host->h_addr, host->h_length);
memset(client.sin_zero, 0 , sizeof(client.sin_zero));
if (connect (sockfd, (struct sockaddr *)&client, sizeof(client) ) < 0 ){
perror("connect"); return -1;
}
pthread_create(&client_thread_receive,NULL,client_receive, &sockfd);
pthread_create(&client_thread_send,NULL,client_send, &sockfd);
while(1);
}