.net core 3.1 Worker Service - service

I am trying to create a TCP Listener as worker service. Any how managed to achieve the flow for Client Request and Server Response. But from browser when I try to browse the Url for the Application debugger hits the action method and writes the response in a stream but not able to return any response from the Main Thread of worker service i.e. ExecuteAsync method.
Any help in this regards would really help min completing this task.
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
Task.Run(() => _serverStatus = _tcpHandler.StartServer().Result).Wait();
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now
+ Environment.NewLine
+ String.Format("Server Started with status : {0}", _serverStatus)
+ Environment.NewLine
+ String.Format("Client Message : {0}", _tcpHandler.GetServerResponse())
+ Environment.NewLine
+ String.Format("Number of Requests recieved : {0}", _tcpHandler.GetRequestCounter()));
// _logger.LogInformation("Server running at: {0}", _tcpHandler.StartServer().Result);
await Task.Delay(1000, stoppingToken);
}
}
public async Task<string> StartServer()
{
string serverResponse = String.Empty;
try
{
await Task.Delay(1000);
// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");
serverResponse = "Status - Active";
// Perform a blocking call to accept requests.
// You could also use server.AcceptSocket() here.
_tcpClient = _tcpListener.AcceptTcpClientAsync().Result;
Console.WriteLine("Connected!");
Console.WriteLine(Environment.NewLine + "Waiting for Requests ...");
Thread t = new Thread(() => {
serverResponse = RequestHandler(_tcpClient).Result;
});
t.Start();
return serverResponse;
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
return "Status - Inactive";
}
}
public async Task<string> RequestHandler(object client)
{
string response = String.Empty;
try
{
// Set the TcpListener on port 13000.
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
// while (true)
//{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also use server.AcceptSocket() here.
TcpClient tcpClient = (TcpClient)client;
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
using (NetworkStream stream = tcpClient.GetStream())
{
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
_requestCounter++;
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
_requestedMessage = data;
Console.WriteLine("Message Received by Server: {0}", data);
// Process the data sent by the client.
data = "Hey ! Client ..." + data.ToUpper();
string xml = Environment.NewLine + "<Messages>"
+ Environment.NewLine + "<Message>"
+ Environment.NewLine + "<Date>" + DateTime.Now.ToString() + "</Date>"
+ Environment.NewLine + "<Text>" + data + "</Text>"
+ Environment.NewLine + "<status>" + "accepted" + "</status>"
+ Environment.NewLine + "<statuscode>" + "1" + "</statuscode>"
+ Environment.NewLine + "</Message>"
+ Environment.NewLine + "</Messages>";
// Send back a response.
byte[] httpHeaders = System.Text.Encoding.ASCII.GetBytes("HTTP/1.1 200 OK");
byte[] httpContentType = System.Text.Encoding.ASCII.GetBytes("Content-Type: text/xml");
byte[] httpContentLength = System.Text.Encoding.ASCII.GetBytes("Content - Length: " + xml.Length);
byte[] newLine = System.Text.Encoding.ASCII.GetBytes(Environment.NewLine);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(xml);
stream.Write(httpHeaders, 0, httpHeaders.Length);
stream.Write(httpContentType, 0, httpContentType.Length);
stream.Write(httpContentLength, 0, httpContentLength.Length);
stream.Write(newLine);
stream.Write(msg, 0, msg.Length);
response = xml;
Console.WriteLine("Reply sent from Server: {0}", data);
}
stream.Close();
}
// Loop to receive all the data sent by the client.
// Shutdown and end connection
tcpClient.Close();
//}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
return response;
}

found solution for getting xml response in browser from a tcp background service, instead of using NetworkStream StremReader will do the job for handling passed arguments and StreamWriter will write a response back to client.

Related

Can't read/write data (TcpClient/NetworkStream)

A former employees used TCP Client & TCP Listener, Thread, and NetworkStream to create a payment server with Unity 32-bit for Unity 32-bit games and payment connections.
(I don't know what I'm talking about either)
BUT Only the first payment is successful, and the second payment cannot be 'Read' even if it is 'Write'.
(Or maybe client server listener(?) is dumb.... ;( )
So I tried debugging, but this comment came up.
'SocketException System.Net.Sockets.SocketException: Blocking operation aborted by calling WSACanceBlockingCall.'
I can't even connect to the clientServer.
It's fatal to the company that only one payment is made.
This is because after the payment, the game proceeds and when the GameLife is exhausted, you have to return to the lobby and pay again.
But "Write" works, but why doesn't it go on after that?
If you are disconnected from the game and payment server, you will be warned that you are disconnected from the server.
I'm so confused. I'm a client developer and I don't understand anything because I think I'm developing a server.
I'm just a three-month-old junior developer...
This is my client Write Code
public void SendMessage()
{
if (socketConnection == null)
{
return;
}
try
{
// Get a stream object for writing.
NetworkStream stream = socketConnection.GetStream();
if (stream.CanWrite)
{
//string clientMessage = "This is a message from one of your clients.";
string clientMessage = string.Format("Charge,{0}",DataManager.instance.payData._Money);
// Convert string message to byte array.
//byte[] clientMessageAsByteArray = Encoding.ASCII.GetBytes(clientMessage);
byte[] clientMessageAsByteArray = Encoding.UTF8.GetBytes(clientMessage);
// Write byte array to socketConnection stream.
stream.Write(clientMessageAsByteArray, 0, clientMessageAsByteArray.Length);
Debug.Log("Client sent his message - should be received by server");
}
}
catch (SocketException socketException)
{
serverState = ServerState.Disconnect;
Debug.Log("Socket exception: " + socketException);
}
}
This is Server Read Code
private void ListenForIncomingRequests()
{
try
{
// Get Local IPv4 Address
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
address = ip.ToString();
}
}
Debug.Log("address : "+ address);
// Create listener on localhost port 8052.
tcpListener = new TcpListener(IPAddress.Parse(address), 50001);
tcpListener.Start();
Debug.Log("Server is listening");
Byte[] bytes = new Byte[1024];
while (true)
{
using (connectedTcpClient = tcpListener.AcceptTcpClient())
{
Debug.Log(connectedTcpClient.ToString());
// Get a stream object for reading
using (NetworkStream stream = connectedTcpClient.GetStream())
{
int length;
// Read incoming stream into byte arrary.
while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
{
var incomingData = new byte[length];
Array.Copy(bytes, 0, incomingData, 0, length);
// Convert byte array to string message.
//string clientMessage = Encoding.ASCII.GetString(incomingData);
string[] message = Encoding.UTF8.GetString(incomingData).Split(',');
string clientMessage = message[0];
string value = message[1];
if (string.IsNullOrEmpty(_clientMessage))
{
_clientMessage = clientMessage;
if (_clientMessage.Equals("Charge"))
{
_clientMessage = string.Empty;
pay.PayEvent(int.Parse(value));
}
}
Debug.Log("client message received as: " + clientMessage);
Debug.Log("_client message received as: " + _clientMessage);
Debug.Log(length);
}
}
}
}
}
catch (SocketException socketException)
{
Debug.Log("SocketException " + socketException.ToString());
}
}

POST multipart/form-data contents out of order

I am trying to build a web server using java sockets, everything is fine except when the browser sends POST request with file attached, when the request is received the content of the file is out of order , the file sent was txt file with line numbers when received the line numbers were out of order. is there any way I can avoid this I want ordered data (see pic 99522 is followed by 99712) THANKs
public class Server{
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8080);
while(true) new Thread(new Client(server.accept())).start();
}
}
class Client implements Runnable {
Socket client;
OutputStream os = new FileOutputStream(new File("File12"));
InputStream in;
OutputStream out;
static ArrayList<Socket> clients = new ArrayList<Socket>();
String index;
String response;
Client(Socket client) throws IOException {
String listOfFiles = "<ol>";
this.client = client;
in = client.getInputStream();
out = client.getOutputStream();
clients.add(client);
for (File file : new File(".").listFiles()) if (file.isFile()) listOfFiles += "<li>" + file.getName() + "</li>";
listOfFiles += "</ol>";
index = "<!DOCTYPE html><html><body><h1>" + new Date() + "</h1><hr>"+listOfFiles+"<form id='upload' enctype='multipart/form-data' method='post' action='/upload'><input id='fileupload' multiple name='myfile' type='file' /><input type='submit' value='submit' id='submit' /></form></body></html>";
response = "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length:"+index.length()+"\r\n" +
"\r\n" +
index;
}
public void run() {
try {
String msg = "";
byte buffer[] = new byte[32*1024];
int read = in.read(buffer);
while(read != -1){
msg = new String(buffer);
System.out.println(msg);
if(msg.startsWith("POST")){
System.err.println("RAN IN POST");
out.write("HTTP/1.1 200 OK\r\n".getBytes());
out.write("Content-Type: text/plain\r\n".getBytes());
out.write(("Content-Length:"+ 4 +"\r\n").getBytes());
out.write("\r\n".getBytes());
out.write("done".getBytes());
}
if(msg.startsWith("GET")){
String path = msg.substring(msg.indexOf("/"), msg.indexOf("HTTP")).trim();
if(path.equals("/")) out.write(response.getBytes());
else {
String fileName = path.substring(1);
fileName = URLDecoder.decode(fileName,"UTF-8");
System.out.println(fileName);
File file = new File(fileName);
if(file.exists()){
System.out.println(file.getName() + " " + file.length());
out.write("HTTP/1.1 200 OK\r\n".getBytes());
out.write("Accept-Ranges: bytes\r\n".getBytes());
out.write(("Transfer-Encoding: chunked\r\n").getBytes());
out.write("Content-Type: application/octet-stream\r\n".getBytes());
out.write(("Content-Disposition: attachment; filename=\""+file.getName()+"\"\r\n").getBytes());
out.write("\r\n".getBytes());
try{
Files.copy(Paths.get(file.getPath()) , out);
}catch(Exception e){
break;
}
}else System.out.println("file not existes");
}
}
out.flush();
os.close();
read = in.read(buffer);
}
System.err.println("closing scoket");
out.close();
in.close();
client.close();
clients.remove(client);
} catch (IOException e) {
e.printStackTrace();
}
}
}
ALL right found the bug I am not clearing the buffer hence it appears again

Using websockets through the simple websockets for webgl asset in unity3d, can connect but can't transmit messages

Just having a problem on my mac trying to send strings over web sockets using this https://www.assetstore.unity3d.com/en/#!/content/38367
Lots of adapted code below from here mainly http://www.codepool.biz/how-to-implement-a-java-websocket-server-for-image-transmission-with-jetty.html and the web socket sharp echotest example.
I can connect but there is no sign of strings in my Jetty server console window (on a ws server running in java(eclipse)).
Iā€™m basically just trying to send a ā€œ1ā€ to my server over a websocket connection with the unity editor (5) at the moment, to prompt the server to start sending PNG files encoded as byte arrays, so I can put them back together in a C# script and apply them to a texture.
this is the script, I want to attach it to a game object like a plane or a cube and display the updating images sent over the web socket from my Jetty server, but at the moment I'm just stuck trying to send a message and see it pop up in my eclipse console window.
using UnityEngine;
using System.Collections;
using System;
public class socketTexture : MonoBehaviour {
// Use this for initialization
IEnumerator Start () {
WebSocket w = new WebSocket(new Uri("ws://192.168.0.149:8080/"));
yield return StartCoroutine(w.Connect());
Debug.Log ("Connected");
w.SendString("I'm client");
w.SendString("1");
while (true)
{
byte[] reply = w.Recv();
if (reply != null)
{
Debug.Log ("Received: "+reply);
var tex = new Texture2D(300, 300, TextureFormat.PVRTC_RGBA4, false);
// Load data into the texture and upload it to the GPU.
tex.LoadRawTextureData(reply);
tex.Apply();
// Assign texture to renderer's material.
GetComponent<Renderer>().material.mainTexture = tex;
}
if (w.Error != null)
{
Debug.LogError ("Error: "+w.Error);
break;
}
yield return 0;
}
w.Close();
}
}
...And the relevant code from the jetty server, but this works, I've tested it with some javascript and I can load the PNGs back into the browser window, so I'm definitely doing something wrong in Unity
#OnWebSocketMessage //part request from websocket client (remote browser)
public void onMessage( String message) {
System.out.println("message");
if (message.equals("1") || message.equals("2") || message.equals("3") || message.equals("4") ) {
System.out.println("Part " + message + " joined");
System.out.println( UIMain.usersPath + "/" + message + ".png" );
final String testVar = ( UIMain.usersPath + "/" + message + ".png" );
task = new FileWatcher( new File(testVar) ) {
protected void onChange( File file ) {
// here we code the action on a change
System.out.println( "File "+ file.getName() +" has changed!" );
try {
File f = new File(testVar);
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bi, "png", out);
ByteBuffer byteBuffer = ByteBuffer.wrap(out.toByteArray());
mSession.getRemote().sendBytes(byteBuffer);
out.close();
byteBuffer.clear();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
Timer timer1 = new Timer(); {
timer1.schedule(task , new Date(), 40 );
}
}
else if (message.equals( "0")) {
zerocounter = zerocounter + 1;
if (zerocounter >= 2) {
task.cancel();
}
}
else if (message.equals( "Hi there, client here")) {
System.out.println( "Client says: " + message );
}
}
Any help would be really appreciated, been lurking on here for years, hopefully getting to the stage soon where I can help out others a bit too.
Benedict
Edit:
This is my console error message in Unity
FormatException: Invalid length. System.Convert.FromBase64String
(System.String s) (at
/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:146)
EchoTest+c__Iterator0.MoveNext () (at
Assets/Example/EchoTest.cs:11)
I'm pretty sure the problem arises from websocket sharp for webgl. I need to send the message as a byte array.
OK So Joakim Erdfelt was right, the server was not configured to handle Byte[] messages. Here's what I added to fix it:
#OnWebSocketMessage
public void onMessage(byte[] buffer, int offset, int length) throws UnsupportedEncodingException {
System.out.println(buffer);
String sFclientOutStr = new String(buffer, "UTF-8");
sFclientOut = Integer.parseInt(sFclientOutStr);
System.out.println(sFclientOut);
if ((sFclientOut > 0) & (sFclientOut < 500)) {
System.out.println("Part " + sFclientOut + " joined");
System.out.println( UIMain.usersPath + "/" + sFclientOutStr + ".png" );
final String testVar = ( UIMain.usersPath + "/" + sFclientOutStr + ".png" );
task = new FileWatcher( new File(testVar) ) {
protected void onChange( File file ) {
// here we code the action on a change
System.out.println( "File "+ file.getName() +" has changed!" );
try {
File f = new File(testVar);
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bi, "png", out);
ByteBuffer byteBuffer = ByteBuffer.wrap(out.toByteArray());
mSession.getRemote().sendBytes(byteBuffer);
out.close();
byteBuffer.clear();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
Timer timer1 = new Timer(); {
timer1.schedule(task , new Date(), 40 );
}
}
else if (sFclientOutStr.equals("0")) {
zerocounter = zerocounter + 1;
if (zerocounter >= 2) {
task.cancel();
}
}
else if (sFclientOutStr.equals( "I'm client")) {
System.out.println( "Client says: " + sFclientOutStr );
}
}
These links helped explain it for me http://www.programcreek.com/java-api-examples/index.php?api=org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage
http://www.eclipse.org/jetty/documentation/current/jetty-websocket-api-annotations.html

Handling HTTP 302 Moved Temporarily requests in netty

I am using netty http client to fetch urls using netty. However, for some urls which are redirecting to some other page, I am unable to fetch the content of the final page using my client. I want to know how to handle 302 redirects in my response handler.
Below is the code used in messageReceived function of my response handler.
#Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if (!readingChunks) {
HttpResponse response = (HttpResponse) e.getMessage();
System.out.println("STATUS: " + response.getStatus());
System.out.println("VERSION: " + response.getProtocolVersion());
System.out.println();
if (!response.getHeaderNames().isEmpty()) {
for (String name: response.getHeaderNames()) {
for (String value: response.getHeaders(name)) {
System.out.println("HEADER: " + name + " = " + value);
}
}
System.out.println();
}
if (response.isChunked()) {
readingChunks = true;
System.out.println("CHUNKED CONTENT {");
} else {
ChannelBuffer content = response.getContent();
if (content.readable()) {
System.out.println("CONTENT {");
System.out.println(content.toString(CharsetUtil.UTF_8));
System.out.println("} END OF CONTENT");
}
}
} else {
HttpChunk chunk = (HttpChunk) e.getMessage();
if (chunk.isLast()) {
readingChunks = false;
System.out.println("} END OF CHUNKED CONTENT");
} else {
System.out.print(chunk.getContent().toString(CharsetUtil.UTF_8));
System.out.flush();
}
}
}

Using .NET to get results from an API

I wanted to use the Rapidshare API in my .NET app, but I am confused on how you send the request and bring back the result. Do you use Winsock or another method?
URLs are like this:
http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=checkfiles_v1&files=288725357&filenames=my_upload.txt
Thanks.
Check out the System.Net namespace, specifically System.Net.WebClient.
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.80).aspx
Use the WebClient Class.
http://msdn.microsoft.com/en-us/library/system.net.webclient%28VS.80%29.aspx
You can use this class to programatically interact with webpage. Here's some example code to log into a website. You can adapt this to interact with their web API:
HttpWebRequest request;
HttpWebResponse response;
CookieContainer cookies;
string url = "http://www.jaxtr.com/user/login.jsp";
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = true;
request.CookieContainer = new CookieContainer();
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
StringBuilder sb = new StringBuilder();
StreamReader reader = new StreamReader(response.GetResponseStream());
while (!reader.EndOfStream)
{
sb.AppendLine(reader.ReadLine());
}
//Get the hidden value out of the form.
String fp = Regex.Match(sb.ToString(), "\"__fp\"\\svalue=\"(([A-Za-z0-9+/=]){4}){1,19}\"", RegexOptions.None).Value;
fp = fp.Substring(14);
fp = fp.Replace("\"", String.Empty);
cookies = request.CookieContainer;
//response.Close();
String requestString = "http://www.jaxtr.com/user/Login.action?tzOffset=6&navigateURL=&refPage=&jaxtrId=" + HttpUtility.UrlEncode(credentials.Username) + "&password=" + HttpUtility.UrlEncode(credentials.Password) + "&Login=Login&_sourcePage=%2Flogin.jsp&__fp="+HttpUtility.UrlEncode(fp);
request = (HttpWebRequest)WebRequest.Create(requestString);
request.CookieContainer = cookies; //added by myself
response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Response from login:" + response.StatusCode);
String messageText = (message.TruncateMessage && message.MessageText.Length > JaxtrSmsMessage.MAX_MESSAGE_LENGTH ? message.MessageText.Substring(JaxtrSmsMessage.MAX_MESSAGE_LENGTH) : message.MessageText);
String messageURL = "http://www.jaxtr.com/user/sendsms?CountryName=" + HttpUtility.UrlEncode(message.CountryName) + "&phone=" + HttpUtility.UrlEncode(message.DestinationPhoneNumber) + "&message=" + HttpUtility.UrlEncode(messageText) + "&bySMS=" + HttpUtility.UrlEncode(message.BySMS.ToString().ToLower());
request = (HttpWebRequest)WebRequest.Create(messageURL);
request.CookieContainer = cookies;
response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Response from send SMS command=" + response.StatusCode);
StringBuilder output = new StringBuilder();
using (Stream s = response.GetResponseStream())
{
StreamReader sr = new StreamReader(s);
while (!sr.EndOfStream)
{
output.AppendLine(sr.ReadLine());
}
}
response.Close();
}
else
{
Console.WriteLine("Client was unable to connect!");
}
}
catch (System.Exception e)
{
throw new SMSDeliveryException("Unable to deliver SMS message because "+e.Message, e);
}
This particular code logs into Jaxtr, a SMS messaging service, and sends an SMS message.