Sign in with
Sign up | Sign in
Your question

Programming error: I need help with this code.

Tags:
  • Networking
  • Connection
  • Programming
Last response: in Networking
Share
September 27, 2014 7:43:50 PM

I sense the problem with accepting connection request in this server progam. Coding in C#.
Stmt which is BOLD text is where the real problem is. Something is blocking the connection request.

I have the SerListner class which is accepts connection request.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace CSEServer1Obj
{
class SerListner
{
public static void Main(string[] args)
{
IPAddress hostIP;
Int16 serverPort;
Socket MS_socket,CS_socket;
EndPoint serverEP;
string hostName = Dns.GetHostName();
hostIP = ForcedIPv4Address(Dns.GetHostAddresses(hostName));
serverPort = Convert.ToInt16("5555");
Console.WriteLine(hostName + " Server by host " + hostIP + " Port Number " + serverPort);
serverEP = new IPEndPoint(hostIP, 5555);
MS_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

MS_socket.Bind(serverEP);
MS_socket.Listen(0);
Console.WriteLine("In Main: trying to Create the Child thread... before while loop...");
Console.ReadKey();

while (true)
{
Console.WriteLine("In loop: trying to accept connection created connection socket..");
Console.ReadKey();
SerService serviceObj = new SerService();
CS_socket = MS_socket.Accept();
Console.WriteLine("In loop: trying to Create the Child thread..");
Console.ReadKey();
Thread childThread = new Thread(new ThreadStart(serviceObj.Service));
serviceObj.CS_Sock = CS_socket;

Console.WriteLine("before thread is started ...");
childThread.Start();
Console.WriteLine("End of programmmmm...");

Console.ReadKey();
}


}

static private IPAddress ForcedIPv4Address(IPAddress[] addresses)
{
foreach (IPAddress address in addresses)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
{
return address;
}
}
return null;
}



}



I I have another class which provide the service upon accepting connection request..
}

More about : programming error code

September 27, 2014 7:45:09 PM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;

namespace CSEServer1Obj
{
class SerService
{
public Socket CS_Sock;
public void Service()
{
try
{
string frmsg = "Hi there client!!";
Console.WriteLine("Child thread starts SERVICE...");
Console.Write("{0}", frmsg);
}
catch
{
Console.WriteLine("Connection closed .....shutdown.... Child thread SERVICE...");
Console.Read();


}
CS_Sock.Close(10); // add more code here
}

}
}
m
0
l
September 27, 2014 7:47:08 PM

I have to run this server program check it. Do I need a client appln? Or can it be run independently ie., without any client program.
m
0
l
September 27, 2014 8:05:04 PM

for 10 years , i am away from programming , but that loop seems a real loop ! No exit , nothing and i guess this fill the memory with serviceObj.Service
m
0
l
!