Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on programming a chat application and I need to create “login to server” as a first step. I wrote a code for client application:
<br />
Imports System.Net<br />
Imports System.Net.Sockets<br />
Imports System.Text<br />
Imports System.IO<br />
Imports System.Threading<br />
Public Class frmLogin<br />
<br />
<br />
    Private Sub btnSignIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSignIn.Click<br />
        If txtServer.Text = "" Or txtUName.Text = "" Then<br />
            MsgBox("Popunite sva polja na formi")<br />
        Else<br />
            Dim tcpClient As New TcpClient()<br />
            tcpClient.Connect(txtServer.Text, 8080)<br />
            Dim sendBytes As Byte()<br />
            sendBytes = Encoding.UTF8.GetBytes("LOGIN>" + txtUName.Text)<br />
            tcpClient.GetStream.Write(sendBytes, 0, sendBytes.Length)<br />
        End If<br />
    End Sub<br />
End Class<br />
;

I also wrote a code for file transfer:

Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Public Class Form1
Private alSockets As ArrayList
Public Sub listenerThread()
Dim tcpListener As New TcpListener(8080)
Dim handlerSocket As Socket
Dim thdstHandler As ThreadStart
Dim thdHandler As Thread
tcpListener.Start()
Do
handlerSocket = tcpListener.AcceptSocket()
If handlerSocket.Connected Then
lbConnections.Items.Add( _
handlerSocket.RemoteEndPoint.ToString() + _
"connected.")
SyncLock (Me)
alSockets.Add(handlerSocket)
End SyncLock
thdstHandler = New ThreadStart(AddressOf _
handlerThread)
thdHandler = New Thread(thdstHandler)
thdHandler.Start()
End If
Loop
End Sub
Public Sub handlerThread()
Dim handlerSocket As Socket
handlerSocket = alSockets(alSockets.Count - 1)
Dim networkStream As NetworkStream = New _
NetworkStream(handlerSocket)
Dim blockSize As Int16 = 1024
Dim thisRead As Int16
Dim dataByte(blockSize) As Byte
SyncLock Me
' Only one process can access the
' same file at any given time
Dim fileStream As Stream
fileStream = File.OpenWrite("e:\upload.txt")
While (True)
thisRead = networkStream.Read(dataByte, _
0, blockSize)
fileStream.Write(dataByte, 0, dataByte.Length)
If thisRead = 0 Then Exit While
End While
fileStream.Close()
End SyncLock
lbConnections.Items.Add("File Written")
handlerSocket = Nothing
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim IPHost As IPHostEntry
IPHost = Dns.GetHostByName(Dns.GetHostName())
lblStatus.Text = "My IP address is " + _
IPHost.AddressList(0).ToString()
alSockets = New ArrayList()
Dim thdListener As New Thread(New ThreadStart _
(AddressOf listenerThread))
thdListener.Start()
End Sub
End Class

I was wondering if it is possible to modify this code for sending and receiving text messages using TCP protocol???
Posted

What do you mean? It already sends text messages via tcp - that code that does the login:

wrote:
sendBytes = Encoding.UTF8.GetBytes("LOGIN>" + txtUName.Text)
tcpClient.GetStream.Write(sendBytes, 0, sendBytes.Length)


sends text, not a file. What part do you not know how to do?
 
Share this answer
 
I'm sorry, I wasn't very clear about my question...

The code for client is ok, but I need to create a server which is going to receive text messages from client, and I'm not sure how to do that... I need help! :)
 
Share this answer
 
see this bit:
wrote:
thisRead = networkStream.Read(dataByte, _
0, blockSize)
fileStream.Write(dataByte, 0, dataByte.Length)


it reads from the socket and saves the data to file. what it reads from the socket are bytes - those could be anything but in this case you have said that its a file. If a client takes a string "Hello", converts it to an array of bytes and sends that, then all the server needs to do is take the byte array it receives from networkStream.Read and convert it back into a string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900