473,387 Members | 1,463 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,387 software developers and data experts.

sockets question about multiple IPs on one computer

175 100+
I've run into a problem with some socket code I have. The problem is that there are computers on my network with multiple IP addresses, and I don't know how to tell the sending socket which IP to send.

For example, I've got one computer with say 192.168.0.1 and 192.168.1.1, and it sends the 1.1 every time, but I want the 0.1 instead ( basically I want to tell it which one to send ).

Any ideas how I can get it to explicitly SEND the IP that I want it to send?

I don't think it matters, but all of the computers are running XP.
Feb 23 '10 #1
1 1811
manontheedge
175 100+
I actually found / discovered the solution to my question in case anyone in the future wants to know.

First, the IP can be set in the Create function, the 4th argument is the IP. I wasn't aware of this, since I've never had to use this arg.

Then, to discover the multiple IPs from the local computer ...
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. #include <winsock2.h>
  6. #pragma comment(lib, "wsock32.lib")
  7.  
  8. int main()
  9. {
  10.     WSADATA WSAData;
  11.  
  12.     // Get local host name
  13.     char szHostName[128] = "";
  14.  
  15.     // Get local IP addresses
  16.     struct sockaddr_in SocketAddress;
  17.     struct hostent     *pHost        = 0;
  18.  
  19.     char aszIPAddresses[10][16]; // maximum of ten IP addresses
  20.  
  21.     for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
  22.     {
  23.         memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
  24.         strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
  25.  
  26.         cout << aszIPAddresses[iCnt] << endl;
  27.     }
  28.  
  29.     // Cleanup
  30.     WSACleanup();
  31.  
  32.     system("pause");
  33. }
  34.  
by the way, I ran this on XP using VS.NET 2003
Feb 23 '10 #2

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

Similar topics

4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
15
by: kernel.lover | last post by:
Hello, i want to know to have multiple clients connects to same server program does following is correct code #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include...
7
by: Bill English | last post by:
How do I send an object from one computer to another? -- I am a 14 year old C# developer, I am completely self taught, so please don't get mad if I ask a stupid question. Thanks.
15
by: mrpolitics | last post by:
So I'm working with PureIRCD (http://sourceforge.net/projects/pure-ircd) and everything was fine untill yesterday when the server crashed. So I did a cold restart and staretd the server back up...
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
2
by: Stressed Out Developer | last post by:
We have an application that has a 200 count loop that does the following: ' Each time thru the loop we pass the next IP Address is a range (aka 192.168.4.50 thru 192.168.4.254) Try If...
2
by: Ronodev.Sen | last post by:
the way my program needs to go is -- 1) open a socket and listen on it 2) moment a client connects to the socket - process some data (by sending it to another machine), get the result and send...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
2
yashg
by: yashg | last post by:
I am building a data backup application in C# using Sockets. It has a server component and a client component. The client is going to upload files to the server through TCP sockets. I've got all...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.