I am trying with a function which can send message to an ip and port using socket
while running this application i am getting an exception as - "{"Only one usage of each socket address (protocol/network address/port)\r\nis normally permitted" }"
Please help ...
regards,
Vinu
the code like this,
String IP will be like this - 192.168.1.6:5763
public void SendToIp(string IP, string Message)
{
try{
string add = IP.Substring(0,IP.IndexOf(":")); int port = int.Parse(IP.Substring(IP.IndexOf(":")+1,IP.Length-IP.IndexOf(":")-1));IPAddress ipad = IPAddress.Parse(add);
Socket sk =
new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);sk.Bind(
new IPEndPoint(ipad,ip)); if(sk.Connected){
sk.Send(StringToByte(Message));
}
}
catch(Exception ex){
Console.WriteLine("\nException is : {0}",ex.Message);
}
}

creating new Socket with IPEndPoint
jnf
Your code seems incomplete. You are supposed to do a Connect to a remote IP before you do a Send. Or you need to specify the remote IP address where you want to send it to. Socket.Bind is the local port where you want the machine to bind to.
TJak
thanks for the help.... It solved my issue. :)
regards,
Vinu.P.K
Angus Leeming
The exception you get from bind is because the port with which you are trying to bind has already been bound to by another socket(May be previous execution of your application which was not terminated- you might want to check your taskmanager for the running processes).
Try netstat -a from your command prompt.. This gives you information regarding ports and the state the ports are in...
ApSav
using
System;using
System.Collections.Generic;using
System.Text;using
System.Net;using
System.Net.Sockets;namespace
SimpleSocket{
class Program{
static void Main(string[] args){
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // you can omit the line below if you don't need to specify the client's port sock.Bind(new IPEndPoint(IPAddress.Any, 5000));sock.Connect(
IPAddress.Parse("ENTER THE IPADDRESS OF THE HOST YOU ARE CONNECTING TO HERE"), 80); if(sock.Connected){ Console.WriteLine("*** Socket Connected ***"); //sock.Send(...);}
sock.Shutdown(
SocketShutdown.Both);sock.Close();
Console.ReadLine();}
}
}
ooartist
Hi Maheshwar,
thnaks for your reply,
so i need to add one more line after this,
Socket sk = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sk.Bind(new IPEndPoint(ipad,ip));
----------------------------------
I tried like this...
Socket sk = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sk.Bind(new IPEndPoint(ipad,ip));
sk.Connect(endpoint);
Still i am getting exception...
can u please help me a sample code for this
regards,
Vinu