#include "stdafx.h"
#include<stdio.h>
#include<winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET ServerSocket , new_socket , clientSockets[100] , progressSocket;
struct sockaddr_in server, address;
int max_clients = 100 , addrlen, i, val;
int totalClient=0,tmp2=0;
bool flag=true;
fd_set fds;
char *buffer;
buffer = (char*) malloc((1024 + 1) * sizeof(char));
for(i = 0 ; i < 100;i++)
{
clientSockets[i] = 0;//toplam soketlerin tutulduğu array
}
if(argc!=2)
{
printf("Please enter port number");
return 0;
}
string arguments=argv[1];
int portNumber=atoi(arguments.c_str());//port numarası aldık
printf("Start\n");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)//winsock.dll kullanıma açtık
{
printf("error : %d",WSAGetLastError());
exit(EXIT_FAILURE);
}
if((ServerSocket = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)//soket olusturduk
{
printf("error socket : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( portNumber );//soket adresi belirledik
if( bind(ServerSocket ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)//ilişkilendirdik
{
printf("error : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
listen(ServerSocket , 3);//dinleme basladı
addrlen = sizeof(struct sockaddr_in);
bool flag2=true;
while(TRUE)
{
FD_ZERO(&fds);
FD_SET(ServerSocket, &fds);
for ( i = 0 ; i < max_clients ; i++)
{
progressSocket = clientSockets[i];
if(progressSocket > 0)//yeni bi eleman geldi
{
FD_SET( progressSocket , &fds);
}
}
if ( select( 0 , &fds , NULL , NULL , NULL) == SOCKET_ERROR )
{
printf("error : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
if (FD_ISSET(ServerSocket , &fds))
{
if ((new_socket = accept(ServerSocket , (struct sockaddr *)&address, (int *)&addrlen))<0)//yeni elemanı kabul ettik
{
exit(EXIT_FAILURE);
}
char message[1024];
sprintf_s(message,"Welcome!! Your IP = %s\r\nIf you want to exit you should write exit\r\n",inet_ntoa(address.sin_addr));
if( send(new_socket,message,strlen(message),0) != strlen(message) ) //cliente hosgeldin mesajı gonderdik
{
printf("error ");
}
for (i = 0; i < max_clients; i++)
{
if (clientSockets[i] == 0)
{
clientSockets[i] = new_socket;//yeni clienti arraya ekledik
printf("client no = %d \n" , i+1);
totalClient++;
break;
}
}
}
for (i = 0; i < max_clients; i++)
{
progressSocket = clientSockets[i];
if (FD_ISSET( progressSocket , &fds))
{
getpeername(progressSocket , (struct sockaddr*)&address , (int*)&addrlen);
val = recv( progressSocket , buffer, 1024, 0);//clientten gelen bilgiyi kabul ettik
if( val == SOCKET_ERROR)// client cikis yaptı
{
if(WSAGetLastError() == WSAECONNRESET)
{
printf("Disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
closesocket( progressSocket );
clientSockets[i] = 0;
}
else
{
printf("error : %d" , WSAGetLastError());
}
}
if ( val == 0)// client cikis yaptı
{
printf("Disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
closesocket( progressSocket );
clientSockets[i] = 0;
}
else
{
buffer[val] = '\0';
int tmp;
for(tmp=0;tmp<totalClient;tmp++)
if(progressSocket==clientSockets[tmp])
if(tmp2!=tmp+1)
{
tmp2=tmp+1;//tmp2 yazismalara icin kontrol maksatlı kullanıldı.
flag=false;
}
else
flag=true;
if(flag==false)//flag false ise farklı bir client mesaj gonderdi kontrolu icin kullanıldı
{
char clientName[1024];
sprintf_s(clientName,"\nClient %d says\r\n",tmp2);
for(tmp=0;tmp<totalClient;tmp++)
if(progressSocket!=clientSockets[tmp])//amac mesajı yazana da send yapmamak icin
send(clientSockets[tmp], clientName, strlen(clientName), 0);
}
for(tmp=0;tmp<totalClient;tmp++)
if(progressSocket!=clientSockets[tmp])
send(clientSockets[tmp], buffer, strlen(buffer), 0);//amac mesajı sadece diger clientlerin gormesi.Diger turlu mesajı yazana da aynı mesaj gidiyo
//send( progressSocket , buffer , val , 0 );
}
}
}
}
closesocket(progressSocket);
WSACleanup();
return 0;
}
Hiç yorum yok:
Yorum Gönder