473,543 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

client/server program

2 New Member
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.averag e,"");
char rcvBuf[2048],ok[2];
strcpy(ok,"OK") ;
recv(client, rcvBuf,2048,0);
printf("%s",rcv Buf);
t=fopen(rcvBuf, "w+");
if(t){
recv(client,ok, sizeof(ok),0);
while(strcmp(ok ,"OK")==0){
recv(client,s.n ame,sizeof(s.na me), 0);
recv(client, s.ID,sizeof(s.I D), 0);
recv(client,s.a verage,sizeof(s .average), 0);
fwrite(&s,sizeo f(s),1,t);

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


recv(client,ok, sizeof(ok),0);
}
fclose(t);
strcpy(rcvBuf," Finish your task.");
send(client,rcv Buf,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",rcv Buf);
t=fopen(rcvBuf, "r");
if(t==NULL){
printf("null");
strcpy(rcvBuf," NOT OK");
send(client,rcv Buf,2048,0);
return ;
}
else if(t!=NULL)
{
strcpy(rcvBuf," OK");
send(client,rcv Buf,2048,0);

strcpy(rcvBuf," ");
recv(client,ok, sizeof(ok),0); //recieve "GO"
while(1){
//printf("%s",ok) ;
if(strcmp(ok,"G O")==0){
fseek(t,0,SEEK_ SET);
//recv(client,ok, sizeof(ok),0);
recv(client,rcv Buf,2048,0);


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

if(strcmp(s.nam e,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,r esult);
send(client,rcv Buf,2048,0);
i=0;
}
else{
strcpy(rcvBuf," this name doesn't exit in server\n");
send(client,rcv Buf,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_fa mily = AF_INET;
saServer.sin_ad dr.s_addr = inet_addr("127. 0.0.1");
saServer.sin_po rt = htons(27015);
version = MAKEWORD(2,2);
WSAStartup(vers ion, &wsaData);
listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

printf("\nServe r listening to port for connection...") ;
if(bind(listeni ngSocket, (SOCKADDR *) &saServer, sizeof(struct sockaddr))== SOCKET_ERROR)
{
printf("\nbindi ng is not successful.");
closesocket(lis teningSocket);
return 0;
}

listen(listenin gSocket, 1);

theClient=INVAL ID_SOCKET;
theClient = accept(listenin gSocket,NULL, NULL);
if(theClient!=I NVALID_SOCKET){
printf("\nA client connect to server.");
while(1){
recv(theClient, rcvBuf, 2048, 0);
strcpy(opcode,r cvBuf);

if(strcmp(opcod e,"1")==0){

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

}
if(strcmp(opcod e,"2")==0){
insert(theClien t);

}

if(strcmp(opcod e,"3")==0){
search(theClien t);

}

}

}

strcpy(rcvBuf," ");
theClient=INVAL ID_SOCKET;
closesocket(the Client);
closesocket(lis teningSocket);
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(SOC KET Client) //insert filename,send it to server
{
char rcvBuf[2048];
printf("please enter your file name:");
scanf("%s",&rcv Buf);
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",rcvB uf);
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(st d.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,rcv Buf,2048,0);
printf("%s",rcv Buf); //message that write data has done successfully.

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

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

}
return;

}
void search(SOCKET client)
{
//student std;
char rcvBuf[2048],ok[2048],x;
printf("please enter your filename:");
scanf("%s",rcvB uf);
send(client, rcvBuf, 2048, 0);
recv(client,rcv Buf,2048,0);
if(strcmp(rcvBu f,"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("\npleas e enter Name:");
scanf("%s",&rcv Buf);
send(client,rcv Buf,2048, 0);
recv(client,rcv Buf,2048,0); //result
printf("%s",rcv Buf);
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(MAKE WORD(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(RemoteAd dr) ) == SOCKET_ERROR)


{printf("\nconn ection failed");}


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

printf("\n1:Cre ate your arbitary file");
printf("\n2:Ins ert data to the especified file");
printf("\n3:Sea rch data in the especified file");

printf("\npleas e enter opcode:");
scanf("%d",&opc ode);
switch(opcode)
{
case 1: //send create file to the server.
strcpy(rcvBuf," 1");
send(sClient,rc vBuf,2048,0);
create_file(sCl ient);
recv(sClient, rcvBuf, 2048, 0);
printf("%s is created.\n",rcv Buf);
break;

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

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

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

}//else

}

closesocket(sCl ient);
WSACleanup();
return 0;
}
Nov 25 '07 #1
2 4084
nsaffary
2 New Member
hi .
My problem solved.
Nov 25 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
2388
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 decided to do it as a Windows Forms client application, hooking up to a central SQL Server, and then maybe later create an asp version of the client...
15
4453
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 computations, the only data transfered is user mouse/kbd input. It works synchronously, but somehow, when I play in client window, both client and ...
4
6562
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. There is no consistency when it fails. Could anyone here shed some light on how to debug/resolve the issue. I guess IBM looked at the issue and were...
3
6895
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 Accpac 5.1 client program (I would like to find an OpenSource generic ODBC program for Wiindows that can do simple SQL). An Accpac database was...
2
2167
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:"; cin.getline(sendBuf,100); rVal = send(theSocket, sendBuf, strlen(sendBuf), 0);
4
6082
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 implement this by MAC address. When user browser the web site I sometime can get user pc MAC and sometime no. Why this type of implementation are so...
4
6504
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 for messages from a client - non threaded import java.io.*;
0
1794
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 error "Segmentation Fault" This is Code for server: /* server_it.c: DST iterative echo server to be linked with DST_sock */ #include...
2
5397
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 Program use IO::Socket::INET; print ">> Server Program <<\n";
0
7402
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7590
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7733
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7344
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7684
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
1814
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
633
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.