|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
VB Socket programming(Server )
I am writing a server in Visual Basic using WINSOCK API.
This server creates a socket and listens at a particular port. The client(C-PROGRAM) will connect to this server and send a rtf file which the server will collect and then the server will covert this rtf file to the ps(postscript) file and sent back to the client. Here i am able to get client connection and also able to convert it to ps and send it back to the client. But this is possible only as a single threaded way. At a time only one client is possible to work with the server and if something goes wrong with any file the server program cannot move further . My requirement is that the server should work with multiple clients at a time. Thus i did write write code in ActiveX EXE with exposed method which recieves the handle of a (client)socket gets the file and sends it back to the client. the code that intitaites the Activex exe class is as follows (only important steps have been pasted here ) 'Call the accept Winsock API function in order to create a new socket after the clint connects the server lngRetValue = accept(lngSocketHandle, udtSocketAddress, lngBufferSize) 'create an object of the ActiveX EXE class and then call one of its method which recieves the handle of the socket new clinet connection socket Set ps_machine_obj = New ps_machine ps_machine_obj.start_session lngRetValue IN the ActiveX exe when the code tries to recieve the data on this socket handle by the following function Private Function IsDataAvailable(lngSocket As Long) As Boolean ' Dim udtRead_fd As fd_set Dim udtWrite_fd As fd_set Dim udtError_fd As fd_set Dim lngSocketCount As Long ' udtRead_fd.fd_count = 1 udtRead_fd.fd_array(1) = lngSocket ' ' MsgBox lngSocket lngSocketCount = vbselect(0&, udtRead_fd, udtWrite_fd, udtError_fd, 0&) ' IsDataAvailable = CBool(lngSocketCount) ' 'MsgBox WSAGetLastError ' ShowErrorMessage Err.LastDllError End Function the vbselect function returns -1. I do not get as why does it generate the socket ERROR(-1). if the same class is added in the project then it works fine. (i am referring to this site URL) Can any one help me...... |
|
#2
|
|||
|
|||
|
If u Note the referece page,u can found the difference between them:
Reference Sample: ...Function IsConnected(ByVal lngSocket As Long) As Boolean ... Your code: ...Function IsDataAvailable(lngSocket As Long) As Boolean ... Good luck! |
|
#3
|
|||
|
|||
|
cleverpig :
i tried it out both ways ByVal anf ByRef it doesn't work. and i also had mentioned that the same class when i include in the project it works fine. if things are not yet clear please ask me i will provide more details. |
|
#4
|
|||
|
|||
|
Your class is same as vbip web page??Can u give me the class work fine,maybe i can find something?!..How do u call the Is DataAvailable function with the parameters??
|
|
#5
|
|||
|
|||
|
Class code is attached:
The function that calls the class is as follows Private Sub cmdAccept_Click() ' Dim lngSocket As Long Dim lngRetValue As Long Dim objProtocol As CProtocol Dim lvItem As ListItem Dim ps_machine_obj As ps_machine ' 'Is there any selected socket in the listview contorl? 'If Not lvSockets.SelectedItem Is Nothing Then ' 'Get the socket handle 'lngSocket = CLng(lvSockets.SelectedItem.Text) If Sockethandle.Text = "" Then Exit Sub End If 'Enable timer Timer1.Enabled = True lngSocket = Sockethandle.Text ' 'Call the vbAccept function in order to accept the 'connection request and create a new socket lngRetValue = vbAccept(lngSocket) 'MsgBox "Accept : " & CStr(lngRetValue) If lngRetValue = INVALID_SOCKET Then ' 'An error was occurred - show the error message Call ShowErrorMessage(Err.LastDllError) ' Else ' 'The vbAccept function has returned a valid socket 'handle. We need to add a new item into the listview 'control for the socket recently created by that function. ' 'Each item in the listview has several columns to display 'the socket's parameters. We can retrieve that parameters 'with CProtocol class. ' 'Create an instance of the CProtocol class Set objProtocol = New CProtocol Call objProtocol.GetBySocketHandle(lngRetValue) ' 'Add a new item into the listview Set lvItem = lvSockets.ListItems.Add(, "S" & CStr(lngRetValue), lngRetValue, , 2) ' 'Add values for the socket properties' columns ' lvItem.SubItems(1) = cboAddressFamily.List(objProtocol.AddressFamily - 1) ' lvItem.SubItems(2) = cboSocketType.List(objProtocol.SocketType - 1) ' lvItem.SubItems(3) = cboProtocol.List(objProtocol.Protocol - 2) ' ' lvItem.SubItems(1) = cboAddressFamily.Text lvItem.SubItems(2) = cboSocketType.Text lvItem.SubItems(3) = cboProtocol.Text 'All the connected sockets are marked with a bold font lvItem.Bold = True ' End If '--> If lngRetValue = INVALID_SOCKET Then ' ' End If '--> If Not lvSockets.SelectedItem Is Nothing Then ' Set objProtocol = Nothing Set ps_machine_obj = New ps_machine ps_machine_obj.start_session lngRetValue Set ps_machine_obj = Nothing 'While Not IsDataAvailable(lngSocket) ' Sleep (2000) 'Wend ' End Sub |
|
#6
|
|||
|
|||
|
Difference between Byval and Byref:
Passing Arguments ByVal or ByRef Arguments can be passed ByRef (the default) or ByVal. ByRef passes a reference to the actual data item; ByVal passes a copy of the data. When you pass a value to a procedure you may pass it ByVal or ByRef (for by value or by reference). ByVal sends a copy of the argument’s value to the procedure. You would use the ByVal when you do not want the procedure to alter the original value. ByRef sends a reference indicating where the value is stored in memory. You would use ByRef when you want the called procedure to actually change the argument's original value. You can specify how you want to pass the argument by using the ByVal or ByRef keyword before the argument. If you don't specify ByVal or ByRef, arguments are passed by reference.. So i think u should modify this statement"lngSocketCount = vbselect(0&, udtRead_fd, udtWrite_fd, udtError_fd, 0&)" to "lngSocketCount = vbselect(0, udtRead_fd&, udtWrite_fd&, udtError_fd&, 0)" |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > VB Socket programming(Server ) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|