473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

client/server program

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
2 4073
hi .
My problem solved.
Nov 25 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Yes? Well, what was it??
Nov 25 '07 #3

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

Similar topics

2
by: Microsoft | last post by:
I'm about to start converting my application from a old-style monolith exe (with flat files and limited database support for sharing some of the data) to a layered .NET SQL server version. I have...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
4
by: Prince Kumar | last post by:
I joined a company recently and they have a java program which hangs (does nothing) after a while. This is no way consistent. It could succeed quite a few times and can fail a few other times....
3
by: Roy Souther | last post by:
Trying to get the unixODBC to work connecting to an IBM DB2 UDB V8.1. The DB2 server is running on Red Hat 8 and is tested and confirmed to be serving connections to Windows 98 clients using the...
2
by: Matt | last post by:
I wrote the tcp socket client-server program that the server will echo the message received from the client. In client program: char sendBuf; while(1) { cout << "Enter message:";...
4
by: Goh | last post by:
Hi, I would like to know how can we implement a web page that intelligent enough to unique identify that pc have been visit before without any cookies and login user require. I have try...
4
by: rajbala | last post by:
Hi all, I am a newbie to JSP. I had a program in client and server by using java. But i want the same program in JSP. Please help me. server: // TCP server which waits...
0
by: ollii | last post by:
Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get...
2
by: fredszky | last post by:
Hello I am very new to perl, however i managed to make this server/client work with udp, now i would like to do the same thing but with TCP/IP, what must i do? Server: #!perl -w # Server...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.