Beruflich Dokumente
Kultur Dokumente
SESSION : 2019-20
1. Write a program to implement Caesar Cipher.
Encryption:
#include<iostream>
#include<string>
using namespace std;
int main(){
cout<<“ Name : NEWTAN KUMAR RAI”<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int i,j,k;
string s,t;
int key;
cout<<"Enter the key\n";
cin>>key;
cout<<"Enter the message\n";
cin>>s;
for(i=0;i<s.size();i++){
t+=(s[i]-'A'+key)%26+'A';
}
cout<<"\nEncrypted message is "<<t<<'\n';
return 0;
}
Decryption:
#include<iostream>
#include<string>
using namespace std;
int main(){
cout<<"Name : NEWTAN KUMAR RAI<<endl";
cout<<"Roll No : 17BCS086"<<endl;
int i,j,k;
string s,t;
int key;
cout<<"Enter the key\n";
cin>>key;
cout<<"Enter the message to decrypt\n";
cin>>s;
for(i=0;i<s.size();i++){
t+=(s[i]-'A'-key+26)%26+'A';
}
cout<<"\nDecrypted message is "<<t<<'\n';
return 0;
}
Encryption:
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<“ Name : NEWTAN KUMAR RAI”<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int t,n,i,j,k,sum=0;
string m;
cout<<"Enter the message"<<'\n';
cin>>m;
string key;
cout<<"Enter the key"<<'\n';
cin>>key;
int mod = key.size();
j=0;
for(i=key.size();i<m.size();i++){
key+=key[j%mod];
j++;
}
string ans="";
for(i=0;i<m.size();i++){
ans += (key[i]-'A'+m[i]-'A')%26+'A';
}
cout<<"Encrypted message: "<<ans<<'\n';
return 0;
}
Decryption:
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<"Name : NEWTAN KUMAR RAI"<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int t,n,i,j,k,sum=0;
string m;
cout<<"Enter the message"<<'\n';
cin>>m;
string key;
cout<<"Enter the key"<<'\n';
cin>>key;
int mod = key.size();
j=0;
for(i=key.size();i<m.size();i++){
key+=key[j%mod];
j++;
}
string ans="";
for(i=0;i<m.size();i++){
ans += (m[i]-key[i]+26)%26+'A';
}
cout<<"Decrypted message: "<<ans<<'\n';
return 0;
}
Encryption:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
cout<<"Name : NEWTAN KUMAR RAI"<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int i,j,k,n;
vector<vector<char>> a(26,vector<char>(26));
k=0;
n=26;
for(i=0;i<n;i++){
k=i;
for(j=0;j<n;j++){
a[i][j]='A'+k;
k++;
if(k==26)
k=0;
}
}
cout<<"Enter the message\n";
string s;
cin>>s;
cout<<"Enter the key\n";
string key;
cin>>key;
k=0;
int mod = key.size();
for(i=key.size();i<s.size();i++){
key+=key[k%mod];
k++;
}
string encrypt;
for(i=0;i<s.size();i++){
encrypt+= a[s[i]-'A'][key[i]-'A'];
}
cout<<"Encrypted message: "<<encrypt<<'\n';
return 0;
}
Decryption:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
cout<<"Name : NEWTAN KUMAR RAI"<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int i,j,k,n;
vector<vector<char>> a(26,vector<char>(26));
k=0;
n=26;
for(i=0;i<n;i++){
k=i;
for(j=0;j<n;j++){
a[i][j]='A'+k;
k++;
if(k==26)
k=0;
}
}
cout<<"Enter the encrypted message\n";
string s;
cin>>s;
cout<<"Enter the key\n";
string key;
cin>>key;
k=0;
for(i=key.size();i<s.size();i++){
key+=key[k];
k++;
}
string decrypt;
for(i=0;i<s.size();i++){
for(j=0;j<n;j++){
if(a[j][key[i]-'A']==s[i]){
decrypt += 'A'+j;
break;
}
}
}
cout<<"Decrypted message: "<<decrypt<<'\n';
return 0;
}
Encryption:
#include<iostream>
#include<vector>
using namespace std;
int main(){
cout<<“ Name : NEWTAN KUMAR RAI”<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int x,y,i,j,k,n;
cout<<"Enter the size of key matrix\n";
cin>>n;
cout<<"Enter the key matrix\n";
int a[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cin>>a[i][j];
}
}
cout<<"Enter the message to encrypt\n";
string s;
cin>>s;
int temp = (n-s.size()%n)%n;
for(i=0;i<temp;i++){
s+='x';
}
k=0;
string ans="";
while(k<s.size()){
for(i=0;i<n;i++){
int sum = 0;
int temp = k;
for(j=0;j<n;j++){
sum += (a[i][j]%26*(s[temp++]-'a')%26)%26;
sum = sum%26;
}
ans+=(sum+'a');
}
k+=n;
}
cout<<ans<<'\n';
return 0;
}
Decryption:
#include<iostream>
#include<vector>
using namespace std;
int modInverse(int a, int m){
a=a%m;
for(int x=-m;x<m;x++)
if((a*x)%m==1)
return x;
}
int main(){
cout<<“ Name : NEWTAN KUMAR RAI”<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int x,y,i,j,k,n;
cout<<"Enter the size of key matrix\n";
cin>>n;
cout<<"Enter the key matrix\n";
vector<vector<int>> a(n, vector<int>(n));
vector<vector<int>> adj(n, vector<int>(n));
vector<vector<int>> inv(n, vector<int>(n));
for(i=0;i<n;i++){
for(j=0;j<n;j++){
cin>>a[i][j];
}
}
if(inverse(a,inv,n)){
cout<<"Inverse exist\n";
}
int f=ans.size()-1;
while(ans[f]=='x'){
f--;
}
for(i=0;i<=f;i++){
cout<<ans[i];
}
cout<<'\n';
return 0;
}
5. Write a program to implement Rail Fence Cipher.
Encryption:
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<“ Name : NEWTAN KUMAR RAI”<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int t,n,m,i,j,k,sum=0;
string s;
cout<<"Enter the message"<<'\n';
cin>>s;
cout<<"Enter key"<<'\n';
cin>>n;
vector<vector<char>> a(n,vector<char>(s.size(),' '));
j=0;
int flag=0;
for(i=0;i<s.size();i++){
a[j][i] = s[i];
if(j==n-1){
flag=1;
}
else if(j==0)
flag=0;
if(flag==0){
j++;
}
else j--;
}
for(i=0;i<n;i++){
for(j=0;j<s.size();j++){
if(a[i][j]!=' ')
cout<<a[i][j];
}
}
cout<<'\n';
return 0;
}
Decryption:
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<“ Name : NEWTAN KUMAR RAI”<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int t,n,m,i,j,k,sum=0;
string s;
cout<<"Enter the message to decrypt"<<'\n';
cin>>s;
cout<<"Enter key"<<'\n';
cin>>n;
j=0;
int flag=0;
for(i=0;i<s.size();i++){
a[j][i] = '0';
if(j==n-1){
flag=1;
}
else if(j==0)
flag=0;
if(flag==0){
j++;
}
else j--;
}
int temp =0;
for(i=0;i<n;i++){
for(j=0;j<s.size();j++){
if(a[i][j]=='0')
a[i][j]= s[temp++];
}
}
flag=0;
j=0;
for(i=0;i<s.size();i++){
cout<<a[j][i];
if(j==n-1){
flag=1;
}
else if(j==0)
flag=0;
if(flag==0){
j++;
}
else j--;
}
cout<<'\n';
return 0;
}
6. Write a program to implement Play Fair Cipher using Client-
Server.
Server:
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include<unistd.h>
using namespace std;
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
string ans;
if(s.size()%2==1)
s+="x";
for(i=0;i<s.size()-1;i++){
if(s[i]==s[i+1])
s[i+1]='x';
}
map<char,pair<int,int>> mp2;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
mp2[a[i][j]] = make_pair(i,j);
}
}
for(i=0;i<s.size()-1;i+=2){
int y1 = mp2[s[i]].first;
int x1 = mp2[s[i]].second;
int y2 = mp2[s[i+1]].first;
int x2 = mp2[s[i+1]].second;
if(y1==y2){
ans+=a[y1][(x1-1)%5];
ans+=a[y1][(x2-1)%5];
}
else if(x1==x2){
ans+=a[(y1-1)%5][x1];
ans+=a[(y2-1)%5][x2];
}
else {
ans+=a[y1][x2];
ans+=a[y2][x1];
}
}
if(ans[ans.size()-1]=='x')
ans[ans.size()-1]='\0';
for(i=1;i<ans.size();i++){
if(ans[i]=='x')
ans[i]=ans[i-1];
}
return ans;
}
string ans;
if(s.size()%2==1)
s+="x";
for(i=0;i<s.size()-1;i++){
if(s[i]==s[i+1])
s[i+1]='x';
}
map<char,pair<int,int>> mp2;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
mp2[a[i][j]] = make_pair(i,j);
}
}
for(i=0;i<s.size()-1;i+=2){
int y1 = mp2[s[i]].first;
int x1 = mp2[s[i]].second;
int y2 = mp2[s[i+1]].first;
int x2 = mp2[s[i+1]].second;
if(y1==y2){
ans+=a[y1][(x1+1)%5];
ans+=a[y1][(x2+1)%5];
}
else if(x1==x2){
ans+=a[(y1+1)%5][x1];
ans+=a[(y2+1)%5][x2];
}
else {
ans+=a[y1][x2];
ans+=a[y2][x1];
}
}
return ans;
}
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n');
// Driver function
int main(){
cout<<"Name : NEWTAN KUMAR RAI"<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
Client:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <iostream>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <vector>
#include <map>
#include<bits/stdc++.h>
#include<unistd.h>
string ans;
if(s.size()%2==1)
s+="x";
for(i=0;i<s.size()-1;i++){
if(s[i]==s[i+1])
s[i+1]='x';
}
map<char,pair<int,int>> mp2;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
mp2[a[i][j]] = make_pair(i,j);
}
}
for(i=0;i<s.size()-1;i+=2){
int y1 = mp2[s[i]].first;
int x1 = mp2[s[i]].second;
int y2 = mp2[s[i+1]].first;
int x2 = mp2[s[i+1]].second;
if(y1==y2){
ans+=a[y1][(x1+1)%5];
ans+=a[y1][(x2+1)%5];
}
else if(x1==x2){
ans+=a[(y1+1)%5][x1];
ans+=a[(y2+1)%5][x2];
}
else {
ans+=a[y1][x2];
ans+=a[y2][x1];
}
}
return ans;
}
string ans;
if(s.size()%2==1)
s+="x";
for(i=0;i<s.size()-1;i++){
if(s[i]==s[i+1])
s[i+1]='x';
}
map<char,pair<int,int>> mp2;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
mp2[a[i][j]] = make_pair(i,j);
}
}
for(i=0;i<s.size()-1;i+=2){
int y1 = mp2[s[i]].first;
int x1 = mp2[s[i]].second;
int y2 = mp2[s[i+1]].first;
int x2 = mp2[s[i+1]].second;
if(y1==y2){
ans+=a[y1][(x1-1)%5];
ans+=a[y1][(x2-1)%5];
}
else if(x1==x2){
ans+=a[(y1-1)%5][x1];
ans+=a[(y2-1)%5][x2];
}
else {
ans+=a[y1][x2];
ans+=a[y2][x1];
}
}
if(ans[ans.size()-1]=='x')
ans[ans.size()-1]='\0';
for(i=1;i<ans.size();i++){
if(ans[i]=='x')
ans[i]=ans[i-1];
}
return ans;
}
string s;
for(int i=0;i<n-1;i++){
s+=buff[i];
}
//cout<<s<<'\n';
string key;
cout<<"Enter the key\n";
cin>>key;
string ans = encrypt(s,key);
bzero(buff, sizeof(buff));
for(int i=0;i<ans.size();i++){
buff[i]=ans[i];
}
int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
SERVER:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define IP_PROTOCOL 0
#define PORT_NO 15050
#define NET_BUF_SIZE 32
#define cipherKey 'S'
#define sendrecvflag 0
#define nofile "File Not Found!"
// driver code
int main()
{
int sockfd, nBytes;
struct sockaddr_in addr_con;
int addrlen = sizeof(addr_con);
addr_con.sin_family = AF_INET;
addr_con.sin_port = htons(PORT_NO);
addr_con.sin_addr.s_addr = INADDR_ANY;
char net_buf[NET_BUF_SIZE];
FILE* fp;
// socket()
sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL);
if (sockfd < 0)
printf("\nfile descriptor not received!!\n");
else
printf("\nfile descriptor %d received\n", sockfd);
// bind()
if (bind(sockfd, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0)
printf("\nSuccessfully binded!\n");
else
printf("\nBinding Failed!\n");
while (1) {
printf("\nWaiting for file name...\n");
fp = fopen(net_buf, "r");
printf("\nFile Name Received: %s\n", net_buf);
if (fp == NULL)
printf("\nFile open failed!\n");
else
printf("\nFile Successfully opened!\n");
while (1) {
// process
if (sendFile(fp, net_buf, NET_BUF_SIZE)) {
sendto(sockfd, net_buf, NET_BUF_SIZE,
sendrecvflag,
(struct sockaddr*)&addr_con, addrlen);
break;
}
// send
sendto(sockfd, net_buf, NET_BUF_SIZE,
sendrecvflag,
(struct sockaddr*)&addr_con, addrlen);
clearBuf(net_buf);
}
if (fp != NULL)
fclose(fp);
}
return 0;
}
7. Write a program to implement message transfer using Client-
Server.
Server:
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Driver function
int main() {
cout<<"Name : NEWTAN KUMAR RAI"<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
Client:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}
int main(){
cout<<"Name : NEWTAN KUMAR RAI"<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
Server:
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Driver function
int main() {
cout<<"Name : NEWTAN KUMAR RAI"<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
Client:
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}
int main(){
cout<<"Name : NEWTAN KUMAR RAI"<<endl;
cout<<"Roll No : 17BCS086"<<endl;
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
Client:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#define IP_PROTOCOL 0
#define IP_ADDRESS "127.0.0.1" // localhost
#define PORT_NO 15050
#define NET_BUF_SIZE 32
#define cipherKey 'S'
#define sendrecvflag 0
// driver code
int main()
{
int sockfd, nBytes;
cout<<"Name : NEWTAN KUMAR RAI"<<endl;
cout<<"Roll No : 17BCS086"<<endl;
struct sockaddr_in addr_con;
struct hostent *server;
server = gethostbyname("Khan-2.local");
bzero((char *) &addr_con, sizeof(addr_con));
addr_con.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&addr_con.sin_addr.s_addr,
server->h_length);
int addrlen = sizeof(addr_con);
addr_con.sin_family = AF_INET;
addr_con.sin_port = htons(PORT_NO);
char net_buf[NET_BUF_SIZE];
FILE* fp;
// socket()
sockfd = socket(AF_INET, SOCK_DGRAM,
IP_PROTOCOL);
if (sockfd < 0)
printf("\nfile descriptor not received!!\n");
else
printf("\nfile descriptor %d received\n", sockfd);
while (1) {
printf("\nPlease enter file name to receive:\n");
scanf("%s", net_buf);
sendto(sockfd, net_buf, NET_BUF_SIZE,
sendrecvflag, (struct sockaddr*)&addr_con,
addrlen);
printf("\nData Received!!!\n");
while (1) {
// receive
clearBuf(net_buf);
nBytes = recvfrom(sockfd, net_buf, NET_BUF_SIZE,
sendrecvflag, (struct sockaddr*)&addr_con,
&addrlen);
// process
if (recvFile(net_buf, NET_BUF_SIZE)) {
break;
}
}
printf("\n-------------------------------\n");
}
return 0; }
10. Write
a program to transfer message using Client-Server between
Computers.
Client:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
while(1){
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd, buffer, strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd, buffer, 255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n", buffer);}
close(sockfd);
return 0;
}
Server:
/* The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
// create a socket
// socket(int domain, int type, int protocol)
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
portno = atoi(argv[1]);
// convert short integer value for port must be converted into network byte order
serv_addr.sin_port = htons(portno);
// This listen() call tells the socket to listen to the incoming connections.
// The listen() function places all incoming connection into a backlog queue
// until accept() call accepts the connection.
// Here, we set the maximum size for the backlog queue to 5.
listen(sockfd,5);
// This accept() function will write the connecting client's address info
// into the the address structure and the size of that structure is clilen.
// The accept() returns a new socket file descriptor for the accepted connection.
// So, the original socket file descriptor can continue to be used
// for accepting new connections while the new socker file descriptor is used for
// communicating with the connected client.
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
// This send() function sends the 13 bytes of the string to the new socket
//send(newsockfd, "Hello, world!\n", 13, 0);
while(1)
{ bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
bzero(buffer,256);
fgets(buffer,255,stdin);
send(newsockfd,buffer,strlen(buffer),0);
}
close(newsockfd);
close(sockfd);
return 0;
}
11.Write a program to implement chat using client-server.
Client:
import sys
import socket
import select
def chat_client():
if(len(sys.argv) < 3) :
print 'Usage : python chat_client.py hostname port'
sys.exit()
host = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
while 1:
socket_list = [sys.stdin, s]
else :
# user entered a message
print "who do you wish to send the mesage to?"
ip=raw_input()
msg = sys.stdin.readline()
msg=ip+" % "+msg
s.send(msg)
sys.stdout.write('[Me] '); sys.stdout.flush()
if __name__ == "__main__":
sys.exit(chat_client())
server:
import sys
import socket
import select
HOST = ''
SOCKET_LIST = []
RECV_BUFFER = 4096
PORT = 9007
def chat_server():
while 1:
# get the list sockets which are ready to be read through select
# 4th arg, time_out = 0 : poll and never block
ready_to_read,ready_to_write,in_error = select.select(SOCKET_LIST,[],[],0)
# at this stage, no data means probably the connection has been broken
broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
# exception
except:
broadcast(server_socket, sock, "Client (%s, %s) is offline\n" % addr)
continue
server_socket.close()
if __name__ == "__main__":
sys.exit(chat_server())
******** THE END ********