NETWORK PROGRAM

I made 2 network programs.one for server and one for client.when i run those programs on the same computer, they work perfectly but as soon as i run them on two different computers connected via LAN cable,they did'nt respond to each other.Can someone figure out the problem...
... not without seeing the code.
this is my server program

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
#include<iostream>
#include<winsock.h>
#include<cstdio>
#include<cstdlib>
#include<string>
//#include"dos.h"

using namespace std;

#define NETWORK_ERROR -1
#define NETWORK_OK 0

int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lp,int n)
{
    WSADATA ws;
    int nret;
    
    WSAStartup(0x0101,&ws);
    
    SOCKET lsocket;
    
    char servername[50];
    cout<<"Enter Your Name : ";
    gets(servername);
    
    cout<<"\nCreating Socket.......";
    /*
    for(int i=0;i<5;i++)
    {
        delay(400);
        cout<<".";
    }
    */
    lsocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(lsocket==INVALID_SOCKET)
    {
        cout<<"\nCould Not Creat Listening Socket.";
        WSACleanup();
        getchar();
        return NETWORK_ERROR;        
    }
    else
        cout<<"Socket Created";
    
    //Use a SOCKADDR_IN struct to fill addr info
    SOCKADDR_IN serverinfo;
    
    serverinfo.sin_family=AF_INET;
    serverinfo.sin_addr.s_addr=inet_addr("127.0.0.1");
    serverinfo.sin_port=htons(2609);
    
    //Bind the socket to our local server addr
    cout<<"\nBinding Socket..........";
    /*
    for(i=0;i<5;i++)
    {
        delay(400);
        cout<<".";
    }
*/

    nret=bind(lsocket,(SOCKADDR *)&serverinfo,sizeof(struct sockaddr));
    if(nret==SOCKET_ERROR)
    {
        cout<<"\nCould Not Bind Listening Socket";
        WSACleanup();
        getchar();
        return NETWORK_ERROR;
    }
    else
        cout<<"Socket Bound";
    
    //Make the bound socket listen.Up to 10 ma wait at any one time
    cout<<"\nPutting Bound Socket to Listening Mode....";
    /*
    for(i=0;i<5;i++)
    {
        delay(400);
        cout<<".";
    }
*/

    nret=listen(lsocket,10);
    if(nret==SOCKET_ERROR)
    {
        cout<<"\nCould Not Attain Listen Mode";
        WSACleanup();
        getchar();
        return NETWORK_ERROR;
    }
    else
        cout<<"Entered Listening Mode";
    //Wait for client
    cout<<"\nWaiting for Client .......";
    
    SOCKET commsocket;
    commsocket=accept(lsocket,NULL,NULL);
    if(commsocket==INVALID_SOCKET)
    {
        cout<<"\nCould Not Setup Connection On Request";
        WSACleanup();
        getchar();
        return NETWORK_ERROR;
    }
    else
    {
        cout<<"\nConnection Setup Successfull";
    }
    
    char sendbuffer[256]="\nThis is server,reporting on port 2609";
    char recvbuffer[256];
    while(1)
    {
        for(int i=0;i<255;i++)
        {
            sendbuffer[i]=0;
            recvbuffer[i]=0;
        }
        cout<<"\n"<<servername<<" :";
        gets(sendbuffer);
        if(strcmpi(sendbuffer,"BYE")==0)
        {
            cout<<"\n\nSwitching off the chat and exiting";
            WSACleanup();
            getchar();
            break;
        }
        else
        {
            nret=send(commsocket,sendbuffer,255,0);
            if(nret==SOCKET_ERROR)
            {
                cout<<"\nUnable to send.Please Retry";
                WSACleanup();
                getchar();
            }
            else
            {
                nret=recv(commsocket,recvbuffer,255,0);
                if(nret==SOCKET_ERROR)
                {
                    cout<<"\nCould Not Hear Client";
                    WSACleanup();
                    getchar();
                }
                else
                {
                    cout<<"\nClient : "<<recvbuffer;
                }
            }
        }
    }
    cout<<"\n\nThank You For Using PG's Application.";
    getchar();
    return NETWORK_OK;
}
And here is my client program


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
#include<iostream>
#include<winsock.h>
#include<cstdio>
#include<string>
using namespace std;

#define NETWORK_ERROR -1
#define NETWORK_OK 0

void ReportError(int,const char *);

int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lp,int n)
{
    WSADATA ws;
    int nret;
    
    WSAStartup(0x1010,&ws);
    SOCKET commsocket;
    
    char clientname[50];
    
    cout<<"Enter Your name : ";
    gets(clientname);
     
    cout<<"\nCreating Socket.....";
    commsocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(commsocket==INVALID_SOCKET)
    {
        cout<<"\nSocket Cannot be created.....";
        WSACleanup();
        getchar();
        return NETWORK_ERROR;
    }
    else
        cout<<"Socket successfully created";
    
    SOCKADDR_IN serverinfo;
    serverinfo.sin_family=AF_INET;
    serverinfo.sin_addr.s_addr=inet_addr("127.0.0.1");
    serverinfo.sin_port=htons(2609);
    
    cout<<"\nConnecting to server program......";
    nret=connect(commsocket,(LPSOCKADDR)&serverinfo,sizeof(struct sockaddr));
    if(nret==SOCKET_ERROR)
    {
        cout<<"\nCannot connect to server......";
        WSACleanup();
        getchar();
        return NETWORK_ERROR;
    }
    else
        cout<<"Connected to server successfully";
    
    char sendbuffer[256]="Client: Hello How you ? ";
    char recvbuffer[256];
    while(1)
    {
        for(int i=0;i<=255;i++)
        {
            sendbuffer[i]=0;
            recvbuffer[i]=0;
        }
        cout<<"\n"<<clientname<<" :";
        gets(sendbuffer);
        if(strcmpi(sendbuffer,"BYE")==0)
        {
            cout<<"\n\nClosing chat application";
            WSACleanup();
            getchar();
            break;
        }
        else
        {
            nret=send(commsocket,sendbuffer,strlen(sendbuffer),0);
            if(nret==SOCKET_ERROR)
            {
                cout<<"\nCannot send data to server.Please Retry";
                WSACleanup();
                getchar();
            }
            else
            {
                nret=recv(commsocket,recvbuffer,255,0);
                if(nret==SOCKET_ERROR)
                {
                    cout<<"\nCannot receive data from server.......";
                    WSACleanup();
                    getchar();
                }
                else
                {
                    cout<<"\nServer:"<<recvbuffer;
                }
            }
        }
    }
    cout<<"\n\nThanks for using PG's Chat Application";
    closesocket(commsocket);
    getchar();
    return NETWORK_OK;
}
You're using the loopback address 127.0.0.1, thats only for that one computer.
then what shound I use? shall I use same address for both program or different adresses for both computers?
Well usually for networking over 2 different computers you use the ip address but since its over LAN you might have to use the internal ip, im not sure. Try both and see how it goes.
I jst figure out a problem. I first computer (XP) is showing that the connection is alright but the laptop (win 7) is showing "unidentified network no network access" although both of them are on same workgroup.
You need to bind to INADDR_ANY instead of the loopback address. This will make the server listen to all network interfaces. The LanMan group has nothing to do with it.
Last edited on
@kwb and @Angeljruiz
thanx now my program is A-okay.
Topic archived. No new replies allowed.