By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

Socket programming

P: 39
hi team,

Am doing socket programming in vb.net. I have used client side program in window application and for server side it was console application. Application is running fine. but when send the data from client side, it was got displayed in server end and server was getting closed. i cant able to get where it was hiking up. please help me.

Client side program
Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports System.data
  3. Imports System.Net.Sockets
  4.  
  5. Public Class Form1
  6.     Dim serverlistener As New TcpClient("localhost", 8585)
  7.     Dim readstream As Stream = serverlistener.GetStream()
  8.  
  9.     Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  10.  
  11.         Try
  12.             serverlistener.SendBufferSize = 256
  13.             Dim str As String
  14.             str = TextBox1.Text
  15.             While str <> ""
  16.                 Dim sendbuff As Byte() = System.Text.Encoding.ASCII.GetBytes(str)
  17.                 readstream.Write(sendbuff, 0, sendbuff.Length)
  18.  
  19.             End While
  20.         Catch ex As Exception
  21.  
  22.         End Try
  23.  
  24.  
  25.     End Sub
  26.  
  27.  
  28. End Class
Server Side
Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports System.Net.Sockets
  3.  
  4. Module Module1
  5.  
  6.     Sub Main()
  7.         Try
  8.             Dim clientlistener As New TcpListener(8585)
  9.             clientlistener.Start()
  10.             Console.WriteLine("")
  11.             Dim mysocket As Socket = clientlistener.AcceptSocket
  12.             Console.WriteLine("")
  13.             Dim writebyte(225) As Byte
  14.             mysocket.Receive(writebyte, writebyte.Length, SocketFlags.None)
  15.             Dim str As String = System.Text.Encoding.ASCII.GetString(writebyte, 0, writebyte.Length).Trim(Microsoft.VisualBasic.ChrW(0))
  16.             Dim i As Integer
  17.             For i = 0 To str.Length
  18.                 Console.WriteLine(str)
  19.                 'mysocket.Receive(writebyte, writebyte.Length, SocketFlags.None)
  20.                 ' str = System.Text.Encoding.ASCII.GetString(writebyte, 0, writebyte.Length).Trim(Microsoft.VisualBasic.ChrW(0))
  21.  
  22.             Next
  23.             'While Not str.StartsWith(".")
  24.  
  25.             '    mysocket.Receive(writebyte, writebyte.Length, SocketFlags.None)
  26.             '    str = System.Text.Encoding.ASCII.GetString(writebyte, 0, writebyte.Length).Trim(Microsoft.VisualBasic.ChrW(0))
  27.  
  28.             'End While
  29.             'Console.WriteLine(str)
  30.  
  31.         Catch ex As Exception
  32.  
  33.         End Try
  34.  
  35.         'Console.Write(str)
  36.  
  37.  
  38.     End Sub
  39.  
  40. End Module
  41.  
Jul 1 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.