Cannot connect using sockets once disconnected!

My program consists of three parts:

SCTrans.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "SCTrans.h"

WSADATA wsaData;
SOCKET m_socket;
SOCKET m_backup;
sockaddr_in con;
SOCKET AcceptSocket;

SCTrans::SCTrans()
{

    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
        printf("Error at WSAStartup()\n");

    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( m_socket == INVALID_SOCKET ) {
        printf( "Error at socket(): %ld\n", WSAGetLastError() );
        WSACleanup();
        return;
    }

	m_backup = m_socket;


}


void SCTrans::connectServer(char *ip,int port)
{
    con.sin_family = AF_INET;
    con.sin_addr.s_addr = inet_addr( ip );
    con.sin_port = htons( port );

    if ( connect( m_socket, (SOCKADDR*) &con, sizeof(con) ) == SOCKET_ERROR) {
        printf( "Failed to connect.\n" );
        WSACleanup();
        return;
    }
}


void SCTrans::startServer(int port)
{
    con.sin_family = AF_INET;
    con.sin_addr.s_addr = inet_addr( "0.0.0.0" );
    con.sin_port = htons( port );

    if ( bind( m_socket, (SOCKADDR*) &con, sizeof(con) ) == SOCKET_ERROR) {
        printf( "Failed to connect.\n" );
        WSACleanup();
        return;
    }

    if ( listen( m_socket, 1 ) == SOCKET_ERROR )
        printf( "Error listening on socket.\n");

}


void SCTrans::waitForClient()
{
		AcceptSocket = SOCKET_ERROR;
        while ( AcceptSocket == SOCKET_ERROR ) {
            AcceptSocket = accept( m_backup, NULL, NULL );
        }
        m_socket = AcceptSocket;
}

int SCTrans::sendData(char *sendbuf)
{
	return send( m_socket, sendbuf, strlen(sendbuf), 0 );
}


int SCTrans::recvData(char *recvbuf,int size)
{
	int sz = recv( m_socket, recvbuf, size, 0 );
	recvbuf[sz] = '\0';
	return sz;
}


void SCTrans::closeConnection()
{
	closesocket(m_socket);
	m_socket = m_backup;
}


void SCTrans::fileReceive(char *filename)
{

	char rec[50] = "";
	
			
	recv( m_socket, filename, 32, 0 );
	send( m_socket, "OK", strlen("OK"), 0 );

	FILE *fw = fopen(filename, "wb");

	int recs = recv( m_socket, rec, 32, 0 );
	send( m_socket, "OK", strlen("OK"), 0 );

	rec[recs]='\0';
	int size = atoi(rec);
				

	while(size > 0)
	{
		char buffer[1030];

		if(size>=1024)
		{
			recv( m_socket, buffer, 1024, 0 );
			send( m_socket, "OK", strlen("OK"), 0 );
			fwrite(buffer, 1024, 1, fw);

		}
		else
		{
			recv( m_socket, buffer, size, 0 );
			send( m_socket, "OK", strlen("OK"), 0 );
			buffer[size]='\0';
			fwrite(buffer, size, 1, fw);
		}


		size -= 1024;

	}

	fclose(fw);

}

void SCTrans::fileSend(char *fpath)
{

	char filename[50];
	int i=strlen(fpath);
	for(;i>0;i--)if(fpath[i-1]=='\\')break;
	for(int j=0;i<=(int)strlen(fpath);i++)filename[j++]=fpath[i];

	ifstream myFile (fpath, ios::in|ios::binary|ios::ate);
	int size = (int)myFile.tellg();
	myFile.close();

	char filesize[10];itoa(size,filesize,10);


	send( m_socket, filename, strlen(filename), 0 );
	char rec[32] = "";recv( m_socket, rec, 32, 0 );

	send( m_socket, filesize, strlen(filesize), 0 );
	recv( m_socket, rec, 32, 0 );

	
	FILE *fr = fopen(fpath, "rb");

	while(size > 0)
	{
		char buffer[1030];

		if(size>=1024)
		{
			fread(buffer, 1024, 1, fr);
			send( m_socket, buffer, 1024, 0 );
			recv( m_socket, rec, 32, 0 );

		}
		else
		{
			fread(buffer, size, 1, fr);
			buffer[size]='\0';
			send( m_socket, buffer, size, 0 );
			recv( m_socket, rec, 32, 0 );
		}


		size -= 1024;

	}

	fclose(fr);

}


SCTrans.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "winsock2.h"
#include <stdio.h>
#include <conio.h>
#include "iostream.h"
#include "fstream.h"


class SCTrans
{

public:
	
	SCTrans();

	void connectServer(char*,int);
	 int sendData(char*);
	 int recvData(char*,int);
	void fileSend(char*);
	void fileReceive(char*);
	void startServer(int);;
	void waitForClient();
	void closeConnection();
};


And the main part, Protocol.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "SCTrans.h"
#include "iostream.h"
#include <sys/stat.h> 
#include <stdio.h>
#include <string.h>


bool filecheck(char file[32]) { 
  struct stat finfo; 
  bool fncreturn; 
  int statres; 
  statres = stat(file,&finfo); 
  if(statres == 0) {  
    fncreturn = true; 
  } else { 
    fncreturn = false; 
  } 
   
  return(fncreturn); 
}


void seed();
void leech();
void track();


SCTrans w;
SCTrans t;

void main()
{
	int select;
	char filename[99];
	cout<<"\t\t\t-------------------------------------\n"<<"\t\t\tWelcome to File Distribution Protocol\n"<<"\t\t\t-------------------------------------\n"; 
reselect:
	cout<<"\n\nWhat would you like to be?\n\n1: SEEDER\n2: LEECHER\n3: TRACKER\n\nEnter Choice: ";
	cin>>select;
    if (select==1) { seed();}
	else if (select==2) { leech(); }
	else if (select==3) { track(); }
	else { cout<<"ERROR: Please select one of the given choices\n\n"; goto reselect; }
}

void menu() 
{
		int select;
	char filename[99];
	cout<<"\n\n\n\t\t\t-------------------------------------\n"<<"\t\t\tFile Distribution Protocol\n"<<"\t\t\t-------------------------------------\n"; 
reselect:
	cout<<"\n\nWhat would you like to be?\n\n1: SEEDER\n2: LEECHER\n3: TRACKER\n\nEnter Choice: ";
	cin>>select;
    if (select==1) { seed();}
	else if (select==2) { leech(); }
	else if (select==3) { track(); }
	else { cout<<"ERROR: Please select one of the given choices\n\n"; goto reselect; }
}

void track() 
{
	t.startServer(22222);
	printf("Tracker Started........\n");
    while (TRUE) {
		// Wait until a client connects
		t.waitForClient();
		printf("Client Connected......\n");

		// Work with client
		while(TRUE)
		{
			char rec[50] = "";
		t.recvData(rec,32);t.sendData("OK");

			if(strcmp(rec,"FileSend")==0)
			{
				char fname[32] ="";
				t.fileReceive(fname);
				printf("File Received.........\n");
			}
			if(strcmp(rec,"EndConnection")==0)break;
			printf("Connection Ended......\n");
		}
		// Disconnect client
		t.closeConnection();
	}

}
void leech()
{
	int transfer=0;
    char filename[32]="";
	printf("Enter the filename: ");cin>>filename;
	// Start Server Daemon
	w.startServer(27015);
	printf("Server Started........\n");

    while (TRUE) {
        
		// Wait until a client connects
		w.waitForClient();
		printf("Seeder Connected......\n");
				while(TRUE)
		{
        char rec[50] = "";
		// Work with client
			w.recvData(rec,32);w.sendData("OK");


char sendstr[32];
strcpy (sendstr,"CHECKFILE ");
strcat (sendstr,filename);
w.recvData(rec,32);w.sendData("OK");

int a=0;char *cmd,*dta;
char *word = strtok(rec," ");
while (word != NULL)
  {
	if ( a==0 ) { cmd=word;	a=1; }
	else if ( a==1 ) { dta=word; a=0; }
    word = strtok (NULL, " ");

}
printf(cmd);
if(strcmp(cmd,"READY2")==0)
{
	w.recvData(rec,32);w.sendData(sendstr);
}
printf(cmd);
w.recvData(rec,32);w.sendData("PROCESSING");
			if(strcmp(cmd,"FileSend")==0)
			{
				printf(dta);
				char fname[32] ="";
				w.fileReceive(fname);
				printf("File Received.........\n"); transfer=0;
			}
			if(strcmp(cmd,"NOTAVAILABLE")==0)
			{
				printf("File not available at the connected peer...\n");
			}
			
			if(strcmp(rec,"EndConnection")==0)break;
			printf("Connection Ended......\n");

		}
		
		// Disconnect client
		w.closeConnection();menu();
	}

}

void seed()
{
	char *ip,*file;
	ip="127.0.0.1";
	file="filemy.txt";
	char rec[32] = "";

	// Connect To Server
	w.connectServer(ip,27015);
	printf("Connected to leecher...\n");
// ########### SOME PROCESSING DONE HERE, REMOVED TO FIT THE MAX. SIZE #######

	// Send Close Connection Signal
	w.sendData("EndConnection"); w.recvData(rec,32);
	printf("Connection ended......\n"); 

// ############## TRYING TO CONNECT TO ANOTHER SERVER ################

    t.connectServer(ip,22222);
	printf("Connected to tracker...\n");
	system("pause");
}


Now I am running three instances of the program, one as a tracker ( function track() ), one as a leecher ( function leech() ) and another one as a seeder ( function seed() ). At first seed() connects to the server produced through leech() and after some processing closes the connection and then attempts to connect to the server produced through track() but fails to connect.

I tried doing a couple of tricks and none of them works, maybe something is wrong with the SCTrans.cpp part but I can't seem to catch anything. Maybe one of you can trace it down.
Topic archived. No new replies allowed.