Your IP : 216.73.216.40


Current Path : /var/www/html/vijayk/1/
Upload File :
Current File : /var/www/html/vijayk/1/server.cpp

#include<iostream>
#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<stdlib.h>

using namespace std;

int main(){
	int s;
	char buf[50],c,msg[51];
	struct sockaddr_in sadd,cadd;
	sadd.sin_family=AF_INET;
	sadd.sin_addr.s_addr=INADDR_ANY;
	sadd.sin_port=htons(7060);
	memset(&(sadd.sin_zero),'\0',8);
	socklen_t len=sizeof(struct sockaddr_in);
	s=socket(AF_INET,SOCK_STREAM,0);
	bind(s,(struct sockaddr *)&sadd,sizeof(sadd));
	listen(s,4);
	cout<<"To close chat, just type \"bye\".";
	int sa=accept(s,(struct sockaddr*)&cadd,&len);
	while(strcmp(buf,"bye")){
		recv(sa,msg,50,0);
		cout<<"\nMessage received : "<<msg;
		cout<<"\n\nEnter message : ";
		cin.get(buf,50,'\n');
		while (cin.get(c) && c != '\n') ;
		send(sa,buf,strlen(buf)+1,0);
	}
	close(sa);
}