C# Server Program not advancing through functions
I am currently writing a server program in visual c#. Her is a copy of my first two functions.
public void Form1_Load(object sender, System.EventArgs e) { IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,10119); //bind to local IP Address... myServer.Bind( ipLocal ); //start listening... myServer.Listen(10); sServerMsg = "Listening to port: "; List1.Items.Add(sServerMsg); } public void OnClientConnect(IAsyncResult asyn) { myServer.Close(); myServer.Accept(); sServerMsg = "Serving client!"; List1.Items.Add(sServerMsg); } My program never gets to the OnClientConnect function and when I put a breakpoint in that function I got a message that says: Breakppoint will not currently be hit. No executable code associated with this line. how do I get my program to move onto this function and the functions following? |
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
|
You should probably post your entire source file, or at least the
parts you think are executable. Probably you call that function from an event which you think is being triggered but really isn't. What does it do if you print "hello going into func" before the call and "hello returned from func" after call? |
public void Form1_Load(object sender, System.EventArgs e)
{ //nConnected = wsTCP.Connect("127.0.0.1", 10119); IPEndPoint ipLocal = new IPEndPoint ( IPAddress.Any ,10119); //bind to local IP Address... myServer.Bind( ipLocal ); //start listening... myServer.Listen(10); sServerMsg = "Listening to port: "; List1.Items.Add(sServerMsg); } public void OnClientConnect(IAsyncResult asyn) { //long requestID = _e1.requestID; // the winsock control has to be in a closed state // before it can do anything else. (like accept a connection) //Winsock1[Index].Close(); // Let the winsock close //System.Windows.Forms.Application.DoEvents(); // accept the connection request // Winsock1[Index].Accept((int)(requestID)); // Define the message and display it in the list box //myServer.Close(); myServer.Accept(); sServerMsg = "Serving client!"; List1.Items.Add(sServerMsg); } public void WaitForData(System.Net.Sockets.Socket soc) { // When the other end has closed the connection, // close server end too //Winsock1[Index].Close(); myServer.Close(); // to continue listening for another connection //Winsock1[Index].Listen(); myServer.Listen(1000); // show the connection in the list box sServerMsg = "Listening to port: "; List1.Items.Add(sServerMsg); } public void OnDataReceived(IAsyncResult asyn) { //int Index = Winsock1.GetIndex((AxMSWinsockLib.AxWinsock)_sender); //long bytesTotal = _e1.bytesTotal; string strTemp = String.Empty; int a = 0; // Dim strTemp2 As String // This event fires when the process on the other end of the connection(i.e the client) // sends data // retreive data from socket //strTemp = wsTCP.GetData(); // Winsock1(Index).GetData(strTemp, null, null); // Winsock1(Index).GetData strTemp2 // The following code separates each part of the string based on the comma // that is present in the incoming string and parses it out into its // respective textbox a = Strings.InStr(1, strTemp, ",", CompareMethod.Binary); txtcoilid.Text = strTemp.Substring(0, a - 1); // after the first parse you must refine the string as everthing after the comma and // repeat the process throughout the entire string strTemp = strTemp.Substring(a + 1- 1); a = Strings.InStr(1, strTemp, ",", CompareMethod.Binary); txtwidth.Text = strTemp.Substring(0, a - 1); strTemp = strTemp.Substring(a + 1- 1); a = Strings.InStr(1, strTemp, ",", CompareMethod.Binary); txtlinespeed.Text = strTemp.Substring(0, a - 1); strTemp = strTemp.Substring(a + 1- 1); a = Strings.InStr(1, strTemp, ",", CompareMethod.Binary); txtexitcoillength.Text = strTemp.Substring(0, a - 1); } Here is all of my code. It will execute the load form1 function but not any of the three after that. What do you mean by : What does it do if you print "hello going into func" before the call and "hello returned from func" after call? What calls are you talking about? I llok forward to hearing back form you and thanks for your interest in helping me out. |
Oh, so OnClientConnect() is an event handler, right?
I thought you meant you were calling that function, but if the system is supposed to call it for you, then I have no idea where the problem would be. I'd suggest you read whatever docs you have for that event, might also check in the form designer on whether that event registered (I'm not sure what object it would be registered to). |
Do you have any idea why the program would not just continue reading on down through the rest of the code. I am fairly new to C#. Where and how would I read up on something like this. I am using just a basic socket and I know that this is one of its functions. Why does the program stop reading code after the load form1 function?......Any ideas?......Again thanks for your help.
|
Well, the program doesn't just start reading at the top, at least
not in C#. It usually starts at a function called main. If you start in the form designer, it should create an empty function called main() for you. From there you call the functions you want. If this is code in a class that is inherited from another socket class, then it just means the parent class isn't calling the function, and maybe you expect it is but it isn't. What is the socket you're talking about? |
I am sorry to keep bothering you but I think I am a bit confused. I am using a socket from the System.Net.Sockets.Socket class that implements the brekeley sockets interface. Is there some type of component that needs to be added to the for to initialize the functions that will not run? I know in vb6 and vb.net you have to add the socket(ususally winsock) to the form. If not, why would the parent class not call these functions?
|
Some components in C# you use by dragging them onto the
form, and others you don't. Unfortunately, I don't know enough about C# to be able to help you much. You might google for something like: System.Net.Sockets.Socket tutorial and see if you can get a step-by-step tutorial to do that. Maybe it'll have pictures you can look at to see what to do. btw, if you know how to do it in vb6, you might try doing it in vb.net, since all the .net languages can communicate with each other. Don't remeber how that's done either, but that's really all I can tell you about C#. |
I really appreciate all of the help that you have given me. Luckily I found what the problem was. In my load function, I need a call to the connect function to initialize a connection. I have one last question that is the only remaining problem that I am having. The following code is the problem:
System.String strTemp = new System.String(chars); WaitForData(m_socWorker ); int a = 0; a = Strings.InStr(1, strTemp, ",", CompareMethod.Binary); txtcoilid.Text = strTemp.Substring(0, a - 1); strTemp = strTemp.Substring(a + 1- 1); a = Strings.InStr(1, strTemp, ",", CompareMethod.Binary); txtwidth.Text = strTemp.Substring(0, a - 1); strTemp = strTemp.Substring(a + 1- 1); a = Strings.InStr(1, strTemp, ",", CompareMethod.Binary); txtlinespeed.Text = strTemp.Substring(0, a - 1); strTemp = strTemp.Substring(a + 1- 1); a = Strings.InStr(1, strTemp, ",", CompareMethod.Binary); txtexitcoillength.Text = strTemp.Substring(0, a - 1); This code comes from my on data recieved function. My strTemp string should pass 4 variables that are separated by a comma. I am trying to look for the comma and place each value into a text box based on where the comma is located. The problem is My string is not being passed. Do you know what may cause this? Also when and if I get this code to work......will I be able to re type it in a text editor for linux and run the program with the same functionality(i.e. recieve a string of four variable). Will I need to change anything? |
Quote:
I'm telling you is remembered from about a year ago. Maybe you could print out the string to see what it's set to at different points in the program. Just backtrack til you find the problem. Also you might want to look into C#'s regular expression classes. Might make it easier, or you may not like them, just a suggestion. Quote:
sytems. You may be able to get a C# to C translator though, not really sure. If your goal is portability you should probably be using either Java, or PHP. |
What about using visual c++ and creating a console application? If I wrote the code this way would I be able to compile the code in linux?
|
If so should it be a console app or a win32 app?
|
C and C++ are portable between os's depending on what libraries you're
using. If it's just the more generic stuff like stdio library or the math library, you can probably compile it on linux. You said you need networking right. I'm almost positive that kind of code won't work on both windows and linux. Because on windows, you're using windows libraries with all of their functions, and the linux libraries have totally different functions. So the networking part you'd have to write all over again. If you need portable networking code, Java is the only language I know of that might work. Not my favorite language for lot's of reasons, but I if I remember right, the networking classes work across os's. Many times when people write networking apps, they only write it for a particular os. Why do you need it to work on windows and linux? |
btw, don't know how much you need the networking stuff, but
if you build a win32 app it won't work in linux. Because you'd be using the win32 gui libraries (same reason as I said before with the networking). Code written for visual c++ console apps goes back to what I said before, if it just uses generic libraries it will be pretty much portable, but it sounds like you need more than that. |
All times are GMT -5. The time now is 03:05 PM. |