Jump to content

Is there a Difference connecting to local from ...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
danbt

danbt

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi All.

I wrote a TCP Server, and is running on my pc. Listening on port: 4444.

I have wrote a client which connects fine.

However, I need a client which runs on a Windows mobile device.

I have tried to use the same code.

It all compiles Ok, but I get the error.

SocketException: No connection could be made as the host actively refused it.

Im using the same IP, and port for the client.

This is my code for the client:

[SIZE=2][COLOR=#0000ff]
[SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Linq;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Collections.Generic;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.ComponentModel;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Data;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Drawing;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Text;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Windows.Forms;[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Additional references[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Net;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Net.Sockets;[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ATpi_System_1._1d[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]partial[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Form1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] : [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Form[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Form1()[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]InitializeComponent();[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]static[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Connect([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] server, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] message)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]try[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Create a TcpClient.[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#008000] 
[/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Int32[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] port = 4444;[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]TcpClient[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] client = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]TcpClient[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](server, port);[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Translate the passed message into ASCII and store it as a Byte array.[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#008000] 
[/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Byte[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][] data = System.Text.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Encoding[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ASCII.GetBytes(message);[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Get a client stream for reading and writing.[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Stream stream = client.GetStream();[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#008000] 
[/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]NetworkStream[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] stream = client.GetStream();[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Send the message to the connected TcpServer. [/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#008000] 
[/COLOR][/SIZE][SIZE=2]stream.Write(data, 0, data.Length);[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MessageBox[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Sent: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + message);[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Receive the TcpServer.response.[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Buffer to store the response bytes.[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#008000] 
[/COLOR][/SIZE][SIZE=2]data = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Byte[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][1024];[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// String to store the response ASCII representation.[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] responseData = [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Empty;[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Read the first batch of the TcpServer response bytes.[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Int32[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] bytes = stream.Read(data, 0, data.Length);[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2]responseData = System.Text.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Encoding[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ASCII.GetString(data, 0, bytes);[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MessageBox[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Received: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + responseData);[/SIZE]
[SIZE=2][/SIZE] 
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// Close everything.[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]client.Close();[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]catch[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]ArgumentNullException[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MessageBox[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"ArgumentNullException: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + e.ToString());[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]catch[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]SocketException[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]MessageBox[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].Show([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"SocketException: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + e.ToString());[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] button1_Click_1([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]EventArgs[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]Connect([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"127.0.0.1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"SELECT * FROM rides"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]

Can someone PLEASE help, this is for a DUE college project.

Thanks

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
common problems for these kind of behavior is given below read through but the explanation is in Another context
http://www.smartftp....efused-f58.html