By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

client/server program

P: 2
hi
I hava a client/server program that run correctly when i run it in one computer(local) but when I run client on a one computer and run server run on another, connection does not stablish.(I set server machine IP for client and server) please guide me?
server :
#include <winsock2.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#pragma comment(lib, "ws2_32");

struct student
{
char name[20];
char ID[20];
char average[20];
};

int create(char *rcvBuf)
{
FILE * t;
printf( "%s\n", rcvBuf);
t=fopen(rcvBuf,"w");
if(t){
fclose(t);
return 1;
}
else return 0;

}
void insert(SOCKET client)
{
FILE *t;
struct student s;
strcpy(s.name,"");
strcpy(s.ID,"");
strcpy(s.average,"");
char rcvBuf[2048],ok[2];
strcpy(ok,"OK");
recv(client, rcvBuf,2048,0);
printf("%s",rcvBuf);
t=fopen(rcvBuf,"w+");
if(t){
recv(client,ok,sizeof(ok),0);
while(strcmp(ok,"OK")==0){
recv(client,s.name,sizeof(s.name), 0);
recv(client, s.ID,sizeof(s.ID), 0);
recv(client,s.average,sizeof(s.average), 0);
fwrite(&s,sizeof(s),1,t);

strcpy(rcvBuf,"write data successfully\n");
send(client,rcvBuf,2048,0);


recv(client,ok,sizeof(ok),0);
}
fclose(t);
strcpy(rcvBuf,"Finish your task.");
send(client,rcvBuf,2048,0);
}
return;
}
void search(SOCKET client)
{
int i=0;
FILE *t;
student s;
char rcvBuf[2048],ok[2048],result[2048];
strcpy(ok,"");
recv(client, rcvBuf, 2048, 0);
//printf("%s",rcvBuf);
t=fopen(rcvBuf,"r");
if(t==NULL){
printf("null");
strcpy(rcvBuf,"NOT OK");
send(client,rcvBuf,2048,0);
return ;
}
else if(t!=NULL)
{
strcpy(rcvBuf,"OK");
send(client,rcvBuf,2048,0);

strcpy(rcvBuf,"");
recv(client,ok,sizeof(ok),0); //recieve "GO"
while(1){
//printf("%s",ok);
if(strcmp(ok,"GO")==0){
fseek(t,0,SEEK_SET);
//recv(client,ok,sizeof(ok),0);
recv(client,rcvBuf,2048,0);


fread(&s,sizeof(s),1,t);
do{

if(strcmp(s.name,rcvBuf)==0){
strcpy(result,"Name:");
strcat(result,s.name);
strcat(result," ID:");
strcat(result,s.ID);
strcat(result," Average:");
i=1;
strcat(result,s.average);
}
}while(fread(&s,sizeof(s),1,t)!=NULL && i==0);

if(i==1){
strcpy(rcvBuf,result);
send(client,rcvBuf,2048,0);
i=0;
}
else{
strcpy(rcvBuf,"this name doesn't exit in server\n");
send(client,rcvBuf,2048,0);
}

}
else
{
return;
}
recv(client,ok,sizeof(ok),0);
}//end while
return ;
}



}

int main(int argc, char** argv) {
int x;
char rcvBuf[2048],sendBuf[2048],opcode[2];
char msg[100]="your record insert in file successfully\n";
WSADATA wsaData;
WORD version;
SOCKET listeningSocket, theClient;
SOCKADDR_IN saServer;
saServer.sin_family = AF_INET;
saServer.sin_addr.s_addr = inet_addr("127.0.0.1");
saServer.sin_port = htons(27015);
version = MAKEWORD(2,2);
WSAStartup(version, &wsaData);
listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

printf("\nServer listening to port for connection...");
if(bind(listeningSocket, (SOCKADDR *) &saServer, sizeof(struct sockaddr))== SOCKET_ERROR)
{
printf("\nbinding is not successful.");
closesocket(listeningSocket);
return 0;
}

listen(listeningSocket, 1);

theClient=INVALID_SOCKET;
theClient = accept(listeningSocket,NULL, NULL);
if(theClient!=INVALID_SOCKET){
printf("\nA client connect to server.");
while(1){
recv(theClient, rcvBuf, 2048, 0);
strcpy(opcode,rcvBuf);

if(strcmp(opcode,"1")==0){

recv(theClient, rcvBuf, 2048, 0);
if(create(rcvBuf)){
strcpy(sendBuf,"your file create successfully\n");
send(theClient,sendBuf,2048,0);
}

}
if(strcmp(opcode,"2")==0){
insert(theClient);

}

if(strcmp(opcode,"3")==0){
search(theClient);

}

}

}

strcpy(rcvBuf,"");
theClient=INVALID_SOCKET;
closesocket(theClient);
closesocket(listeningSocket);
WSACleanup();

return 0;
}

client:
#include <winsock2.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#pragma comment(lib, "ws2_32");
struct student
{
char name[20];
char ID[20];
char average[20];
};

void create_file(SOCKET Client) //insert filename,send it to server
{
char rcvBuf[2048];
printf("please enter your file name:");
scanf("%s",&rcvBuf);
send(Client, rcvBuf, 2048, 0);

}
void insert(SOCKET Client) //input data,send them to the server for inserting.
{
student std;
char rcvBuf[2048],ok[2],x;
strcpy(ok,"OK");
printf("please enter your filename:");
scanf("%s",rcvBuf);
send(Client, rcvBuf, 2048, 0);
x=getch();
while(1){
if(x!='q'){ //input "q" to scape the loop
send(Client,ok,sizeof(ok),0);
printf("Name:");
scanf("%s",&std.name);
send(Client,std.name,sizeof(std.name), 0);

printf("ID:");
scanf("%s",&std.ID);
send(Client,std.ID,sizeof(std.ID), 0);

printf("Average:");
scanf("%s",&std.average);
send(Client,std.average,sizeof(std.average), 0);

recv(Client,rcvBuf,2048,0);
printf("%s",rcvBuf); //message that write data has done successfully.

x=getch();
}
else
{
strcpy(ok,"NO");
send(Client,ok,sizeof(ok),0);

recv(Client,rcvBuf,2048,0);
printf("%s",rcvBuf); // task finished
break;
}

}
return;

}
void search(SOCKET client)
{
//student std;
char rcvBuf[2048],ok[2048],x;
printf("please enter your filename:");
scanf("%s",rcvBuf);
send(client, rcvBuf, 2048, 0);
recv(client,rcvBuf,2048,0);
if(strcmp(rcvBuf,"NOT OK")==0){
printf("this file not exit in system.");
return;
}
else{
x='h';//input "q" to scape the loop
while(1){

if(x!='q'){
strcpy(ok,"GO");
send(client,ok,sizeof(ok),0);
printf("\nplease enter Name:");
scanf("%s",&rcvBuf);
send(client,rcvBuf,2048, 0);
recv(client,rcvBuf,2048,0); //result
printf("%s",rcvBuf);
x=getch();
}
else if(x=='q'){
strcpy(ok,"not go");
send(client,ok,sizeof(ok),0);
return;
}
}//end while

}
return;
}


int main( int argc, char** argv){
char sendBuf[2048];
char rcvBuf[2048];
int opcode;
WSADATA wsaData;
SOCKET sClient;
int connected;
sockaddr_in RemoteAddr; //Creating an object of type sockaddr_in
RemoteAddr.sin_family = AF_INET; //assigning an Address Family
RemoteAddr.sin_addr.s_addr = inet_addr("127.0.0.1");//is the remote IP address of the server that the client will connect to
RemoteAddr.sin_port = htons(27015);
WSAStartup(MAKEWORD(2,2), &wsaData);
sClient=socket(AF_INET, SOCK_STREAM, 0);
connected = 1;//assigning a Port

/* if ( connect( sClient, //socket name
(SOCKADDR*) &RemoteAddr, //address 2 assign 2 the socket from "sockaddr" structure
sizeof(RemoteAddr) ) == SOCKET_ERROR)


{printf("\nconnection failed");}


*/
//////////////////////////
while(connected){
connected = connect(sClient, (SOCKADDR *) &RemoteAddr, sizeof(SOCKADDR));
if(!connected)
{
printf("connection is stablished.\n");
strcpy(sendBuf,"");
while(1){

printf("\n1:Create your arbitary file");
printf("\n2:Insert data to the especified file");
printf("\n3:Search data in the especified file");

printf("\nplease enter opcode:");
scanf("%d",&opcode);
switch(opcode)
{
case 1: //send create file to the server.
strcpy(rcvBuf,"1");
send(sClient,rcvBuf,2048,0);
create_file(sClient);
recv(sClient, rcvBuf, 2048, 0);
printf("%s is created.\n",rcvBuf);
break;

case 2: //send insert command to the server.
strcpy(rcvBuf,"2");
send(sClient,rcvBuf,2048,0);
insert(sClient);
break;

case 3: //send search command to the server
strcpy(rcvBuf,"3");
send(sClient,rcvBuf,2048,0);
search(sClient);
break;
}//switch

}//while
}//if
else
{
printf("connection is not stablished. %d\n",connected);
return 0;
closesocket(sClient);
WSACleanup();

}//else

}

closesocket(sClient);
WSACleanup();
return 0;
}
Nov 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: 2
hi .
My problem solved.
Nov 25 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Yes? Well, what was it??
Nov 25 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.