How to opening sockets on Different internet connections....!!!!

Hi i am using C# I want to open 2 sockets on 2 different network adapters
1- 3G EVDO USB device
2- USB WIFI STICK (TP-LINK)

Note this is client side program.

i have opened one socket on each adapter one on port 8221 and the other on port 8222, but only one gets connected (the one on 3G EVDO USB device) the other doesn't
the code is given below please tell me how can i be able to open both sockets simultaneously..

button1 = USB WIFI STICK SOCKET
button2 = 3G EVDO USB device socket


[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace Client_test_program
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


Socket m_socWorker;
Socket m_socWorker2;
bool pressed = false;



private void button1_Click(object sender, EventArgs e)
{
try
{

//create a new client socket ...
m_socWorker = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
String szIPSelected = textBox1.Text;
String szPort = textBox2.Text;
int alPort = System.Convert.ToInt16 (szPort,10);

IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Parse("192.168.2.13"),alPort);



System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
m_socWorker.Bind(ipLocal);
m_socWorker.Connect(remoteEndPoint);


}
catch (System.Net.Sockets.SocketException se)
{
MessageBox.Show ( se.Message );
}

}

private void button2_Click(object sender, EventArgs e)
{
try
{
pressed = true;
//create a new client socket ...
m_socWorker2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
String szIPSelected = textBox3.Text;
String szPort = textBox4.Text;
int alPort = System.Convert.ToInt16 (szPort,10);

IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Parse("59.103.195.243"),alPort);



System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
m_socWorker2.Bind(ipLocal);
m_socWorker2.Connect(remoteEndPoint);


}
catch (System.Net.Sockets.SocketException se)
{
MessageBox.Show ( se.Message );
}

}

private void button3_Click(object sender, EventArgs e)
{
try
{
/*
if (openSource.ShowDialog() == DialogResult.OK)
{
m_socWorker.SendFile(openSource.FileName);
}
* */
Object objData = textBox5.Text;
byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());

try
{
m_socWorker.Send(byData);
}
catch (Exception ex)
{

}
if (pressed)
{
m_socWorker2.Send(byData);
}
}
catch (System.Net.Sockets.SocketException se)
{
//MessageBox.Show(se.Message);
}
}

private void button4_Click(object sender, EventArgs e)
{
try
{
byte[] buffer = new byte[1024];
int iRx = m_socWorker.Receive(buffer);
char[] chars = new char[iRx];

System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
textBox6.Text = szData;
}
catch (System.Net.Sockets.SocketException se)
{
MessageBox.Show(se.Message);
}
}

private void button5_Click(object sender, EventArgs e)
{
try
{
m_socWorker.Close();
}
catch (Exception ex)
{

}
}

private void button6_Click(object sender, EventArgs e)
{
try
{
m_socWorker2.Close();
}
catch (Exception ex)
{

}
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
try
{
m_socWorker.Close();
m_socWorker2.Close();
}
catch (Exception ex)
{

}
this.Close();
}




private void button7_Click(object sender, EventArgs e)
{
if (openSource.ShowDialog() == DialogResult.OK)
{
txt_source_file.Text = openSource.FileName;
}

}

private void button8_Click(object sender, EventArgs e)
{
if (saveToFolder.ShowDialog() == DialogResult.OK)
{
SplitFile(txt_source_file.Text, saveToFolder.SelectedPath, (int)numOutputs.Value);
}
}








private void SplitFile(string FileInputPath, string FolderOutputPath, int OutputFiles)
{
// Store the file in a byte array
Byte[] byteSource = System.IO.File.ReadAllBytes(FileInputPath);
// Get file info
FileInfo fiSource = new FileInfo(txt_source_file.Text);
// Calculate the size of each part
int partSize = (int)Math.Ceiling((double)(fiSource.Length / OutputFiles));
// The offset at which to start reading from the source file
int fileOffset = 0;

// Stores the name of each file part
string currPartPath;
// The file stream that will hold each file part
FileStream fsPart;
// Stores the remaining byte length to write to other files
int sizeRemaining = (int)fiSource.Length;

// Loop through as many times we need to create the partial files
for (int i = 0; i < OutputFiles; i++)
{
// Store the path of the new part
currPartPath = FolderOutputPath + "\" + fiSource.Name + "." + String.Format(@{0:D4}, i) + ".part";
// A filestream for the path
if (!File.Exists(currPartPath))
{
fsPart = new FileStream(currPartPath, FileMode.CreateNew);
// Calculate the remaining size of the whole file
sizeRemaining = (int)fiSource.Length - (i * partSize);
// The size of the last part file might differ because a file doesn't always split equally
if (sizeRemaining < partSize)
{
partSize = sizeRemaining;
}
fsPart.Write(byteSource, fileOffset, partSize);
fsPart.Close();
fileOffset += partSize;
}
}
}

private void Join_Click(object sender, EventArgs e)
{

if (saveOutput.ShowDialog() == DialogResult.OK)
{
JoinFiles(txt_source_folder.Text, saveOutput.FileName);
}
}

private void JoinFiles(string FolderInputPath, string FileOutputPath)
{
DirectoryInfo diSource = new DirectoryInfo(FolderInputPath);
FileStream fsSource = new FileStream(FileOutputPath, FileMode.Append);

foreach (FileInfo fiPart in diSource.GetFiles(@*.part))
{
Byte[] bytePart = System.IO.File.ReadAllBytes(fiPart.FullName);
fsSource.Write(bytePart, 0, bytePart.Length);
}
fsSource.Close();
}

private void button9_Click(object sender, EventArgs e)
{
if (openFolder.ShowDialog() == DialogResult.OK)
{
txt_source_folder.Text = openFolder.SelectedPath;
}
}

private void openSource_FileOk(object sender, CancelEventArgs e)
{

}

private void button10_Click(object sender, EventArgs e)
{

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}






}


}

[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories