TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Summit Rajput
NA
41
18.9k
WebSocket server does't connect with client on different PC
Sep 13 2017 5:14 AM
I created a Websocket server on .netcore console application,If i run both client and server on same PC then connection made successful,But if i run my client application on another PC then it shows the error ;-
"Exception: System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server ---> System.Net.WebSockets.WebSock
etException (0x80004005): Unable to connect to the remote server ---> System.Net.Http.WinHttpException (0x80072EFD): A connection with the
server could not be established
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at System.Net.WebSockets.WinHttpWebSocket.<ConnectAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.WebSocketHandle.<ConnectAsyncCore>d__20.MoveNext()
at System.Net.WebSockets.WebSocketHandle.<ConnectAsyncCore>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.ClientWebSocket.<ConnectAsyncCore>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Client_WebSocket.Program.<Connect>d__2.MoveNext() in E:\Learn_Project\Client_WebSocket\Program.cs:line 28"
Client Code:-
using
System
;
using
System
.
Net
.
WebSockets
;
using
System
.
Text
;
using
System
.
Threading
;
using
System
.
Threading
.
Tasks
;
namespace
Client_WebSocket
{
class
Program
{
private
static
UTF8Encoding
encoding =
new
UTF8Encoding
();
static
void
Main
(
string
[] args)
{
Connect
(
"ws://192.168.15.177:8080/"
).
Wait
();
Console
.
WriteLine
(
"Press any key to exit..."
);
Console
.
ReadKey
();
}
public
static
async
Task
Connect
(
string
uri)
{
Thread
.
Sleep
(
1000
);
//wait for a sec, so server starts and ready to accept connection..
ClientWebSocket
webSocket =
null
;
try
{
webSocket
=
new
ClientWebSocket
();
await
webSocket
.
ConnectAsync
(
new
Uri
(
uri
),
CancellationToken
.
None
);
await
Task
.
WhenAll
(
Receive
(
webSocket
),
Send
(
webSocket
));
}
catch
(
Exception
ex)
{
Console
.
WriteLine
(
"Exception: {0}"
,
ex
);
}
finally
{
if
(
webSocket
!=
null
)
webSocket
.
Dispose
();
Console
.
WriteLine
();
Console
.
WriteLine
(
"WebSocket closed."
);
}
}
private
static
async
Task
Send
(
ClientWebSocket
webSocket)
{
while
(
webSocket
.
State
==
WebSocketState
.
Open
)
{
Console
.
WriteLine
(
"Write some to send over to server.."
);
string
stringtoSend =
Console
.
ReadLine
();
byte
[] buffer =
encoding
.
GetBytes
(
stringtoSend
);
await
webSocket
.
SendAsync
(
new
ArraySegment
<
byte
>(
buffer
),
WebSocketMessageType
.
Binary
,
false
,
CancellationToken
.
None
);
Console
.
WriteLine
(
"Sent: "
+
stringtoSend
);
await
Task
.
Delay
(
1000
);
}
}
private
static
async
Task
Receive
(
ClientWebSocket
webSocket)
{
byte
[] buffer =
new
byte
[
1024
];
while
(
webSocket
.
State
==
WebSocketState
.
Open
)
{
var
result =
await
webSocket
.
ReceiveAsync
(
new
ArraySegment
<
byte
>(
buffer
),
CancellationToken
.
None
);
if
(
result
.
MessageType
==
WebSocketMessageType
.
Close
)
{
await
webSocket
.
CloseAsync
(
WebSocketCloseStatus
.
NormalClosure
,
string
.
Empty
,
CancellationToken
.
None
);
}
else
{
Console
.
WriteLine
(
"Receive: "
+
Encoding
.
UTF8
.
GetString
(
buffer
).
TrimEnd
(
'\0'
));
}
}
}
}
}
Server Code:-
using
System
;
using
Server_WebSocket
;
namespace
Server_WebSocket
{
class
Program
{
static
void
Main
(
string
[] args)
{
WebsocketServer
websocketServer =
new
WebsocketServer
();
websocketServer
.
Start
(
"http://localhost:8080/"
);
Console
.
WriteLine
(
"Press any key to exit..."
);
Globals
.
_IsFstTymLoad
=
true
;
Console
.
ReadKey
();
}
}
}
Start Method(Server):-
public
async
void
Start
(
string
httpListenerPrefix)
{
HttpListener
httpListener =
new
HttpListener
();
httpListener
.
Prefixes
.
Add
(
httpListenerPrefix
);
//httpListener.IgnoreWriteExceptions = true;
httpListener
.
Start
();
Console
.
WriteLine
(
"Sevrer Listening..."
);
while
(
true
)
{
//Waiting for Listening client ........
HttpListenerContext
httpListenerContext =
await
httpListener
.
GetContextAsync
();
if
(
httpListenerContext
.
Request
.
IsWebSocketRequest
)
{
ProcessRequest
(
httpListenerContext
);
}
else
{
httpListenerContext
.
Response
.
StatusCode
=
400
;
httpListenerContext
.
Response
.
Close
();
}
}
}
Reply
Answers (
1
)
Save image using ajax method
Which Database use in Desktop Application in .Net.