Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to connect client server using IP address and need to capture the data. I am able to connect the server but i am not able to capture the data. How could i do this.

Could any one help me to sort out this issue.

Thanks in Advance,
Jai
Posted
Comments
HimanshuJoshi 14-Oct-10 13:21pm    
Please post the code that sends data from client and that receives data in server. Also please post what is the exact error you are getting.

The error is that you haven't assigned any network stream to the Receiver in the Connect_to_Server. Only Writer is assigned. Since Receiver is not assigned anything it will not be referencing any network stream. Point your receiver to the network stream you got from the Connected TcpClient.
 
Share this answer
 
Hi Joshi,

Thanks for quick reply. Below is the code.

Error while i am trying to capture the data "Object reference not set to an instance"



using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Client
{
class Program
{
public static bool IsConnected;
public static NetworkStream Receiver;

public static NetworkStream Writer;

static void Main(string[] args)
{

Connect_to_Server();
Read_Command();

}

public static void Connect_to_Server()
{

TcpClient Connector = new TcpClient();

GetConnection:

Console.WriteLine("Enter server IP :");

string IP = Console.ReadLine();

try

{
Connector.Connect(IP, 2000);

IsConnected = true;

Writer = Connector.GetStream();

}

catch

{

Console.WriteLine("Error connecting to target server! Press any key to try again.");

Console.ReadKey();

Console.Clear();

goto GetConnection;

}

//Let user know they connected and that if they type HELP they'll get a list of commands to use.
Console.WriteLine("Connection successfully established to " + IP + ".");
}

public static void Read_Command()
{
while (true)
{
try
{
byte[] RecPacket = new byte[1000];

Receiver.Read(RecPacket, 0, RecPacket.Length);

Receiver.Flush();

string Command = Encoding.ASCII.GetString(RecPacket);

string[] CommandArray = System.Text.RegularExp<B></B>ressions.Regex.Split(Command, "@");

Command = CommandArray[0];

switch (Command)
{


case "MESSAGE":


string Msg = CommandArray[1];

System.Windows.Forms.MessageBox.Show(Msg.Trim('\0'));

break;

case "OPENSITE":

string Site = CommandArray[1];

System.Diagnostics.Process IE = new System.Diagnostics.Process();

IE.StartInfo.FileName = "iexplore.exe";

IE.StartInfo.Arguments = Site.Trim('\0');

IE.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;

IE.Start();

break;
}

}
catch
{

break;
}

}
}

}}
 
Share this answer
 
Comments
HimanshuJoshi 14-Oct-10 14:47pm    
Please do not post your reply as answer. Update your question and use comments section to reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900