TCP client and TCP server...

i m doing a project in GUI form, where the client need to enter data in text field and the data will be pass to server to process, for example if client click button "ADD" then a particular data will pass to server and do sth (save data into a file), or if client click button "DELETE", the client is actually call server to delete a particular file stored in server, how can i do it using TCP socket ??

Hi,
Have you debug these two applications? Does it go into the catch exception block when you close the client or the server?
According to my test, it will throw an exception when the client or the server is closed, just log the exception message in the catch block and then you'll get it:
private async void Process()
try
while (true)
var data = await this.Receive();
this.NewMessage.Invoke(Encoding.ASCII.GetString(data));
catch (Exception exception)
Console.WriteLine(exception.Message);
Unable to read data from the transport connection: An existing   connection was forcibly closed by the remote host.
By the way, I don't know what the SafeInvoke method is, it may be an extension method, right? I used Invoke instead to test it.
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey.

Similar Messages

  • Async tcp client and server. How can I determine that the client or the server is no longer available?

    Hello. I would like to write async tcp client and server. I wrote this code but a have a problem, when I call the disconnect method on client or stop method on server. I can't identify that the client or the server is no longer connected.
    I thought I will get an exception if the client or the server is not available but this is not happening.
    private async void Process()
    try
    while (true)
    var data = await this.Receive();
    this.NewMessage.SafeInvoke(Encoding.ASCII.GetString(data));
    catch (Exception exception)
    How can I determine that the client or the server is no longer available?
    Server
    public class Server
    private readonly Dictionary<IPEndPoint, TcpClient> clients = new Dictionary<IPEndPoint, TcpClient>();
    private readonly List<CancellationTokenSource> cancellationTokens = new List<CancellationTokenSource>();
    private TcpListener tcpListener;
    private bool isStarted;
    public event Action<string> NewMessage;
    public async Task Start(int port)
    this.tcpListener = TcpListener.Create(port);
    this.tcpListener.Start();
    this.isStarted = true;
    while (this.isStarted)
    var tcpClient = await this.tcpListener.AcceptTcpClientAsync();
    var cts = new CancellationTokenSource();
    this.cancellationTokens.Add(cts);
    await Task.Factory.StartNew(() => this.Process(cts.Token, tcpClient), cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
    public void Stop()
    this.isStarted = false;
    foreach (var cancellationTokenSource in this.cancellationTokens)
    cancellationTokenSource.Cancel();
    foreach (var tcpClient in this.clients.Values)
    tcpClient.GetStream().Close();
    tcpClient.Close();
    this.clients.Clear();
    public async Task SendMessage(string message, IPEndPoint endPoint)
    try
    var tcpClient = this.clients[endPoint];
    await this.Send(tcpClient.GetStream(), Encoding.ASCII.GetBytes(message));
    catch (Exception exception)
    private async Task Process(CancellationToken cancellationToken, TcpClient tcpClient)
    try
    var stream = tcpClient.GetStream();
    this.clients.Add((IPEndPoint)tcpClient.Client.RemoteEndPoint, tcpClient);
    while (!cancellationToken.IsCancellationRequested)
    var data = await this.Receive(stream);
    this.NewMessage.SafeInvoke(Encoding.ASCII.GetString(data));
    catch (Exception exception)
    private async Task Send(NetworkStream stream, byte[] buf)
    await stream.WriteAsync(BitConverter.GetBytes(buf.Length), 0, 4);
    await stream.WriteAsync(buf, 0, buf.Length);
    private async Task<byte[]> Receive(NetworkStream stream)
    var lengthBytes = new byte[4];
    await stream.ReadAsync(lengthBytes, 0, 4);
    var length = BitConverter.ToInt32(lengthBytes, 0);
    var buf = new byte[length];
    await stream.ReadAsync(buf, 0, buf.Length);
    return buf;
    Client
    public class Client
    private TcpClient tcpClient;
    private NetworkStream stream;
    public event Action<string> NewMessage;
    public async void Connect(string host, int port)
    try
    this.tcpClient = new TcpClient();
    await this.tcpClient.ConnectAsync(host, port);
    this.stream = this.tcpClient.GetStream();
    this.Process();
    catch (Exception exception)
    public void Disconnect()
    try
    this.stream.Close();
    this.tcpClient.Close();
    catch (Exception exception)
    public async void SendMessage(string message)
    try
    await this.Send(Encoding.ASCII.GetBytes(message));
    catch (Exception exception)
    private async void Process()
    try
    while (true)
    var data = await this.Receive();
    this.NewMessage.SafeInvoke(Encoding.ASCII.GetString(data));
    catch (Exception exception)
    private async Task Send(byte[] buf)
    await this.stream.WriteAsync(BitConverter.GetBytes(buf.Length), 0, 4);
    await this.stream.WriteAsync(buf, 0, buf.Length);
    private async Task<byte[]> Receive()
    var lengthBytes = new byte[4];
    await this.stream.ReadAsync(lengthBytes, 0, 4);
    var length = BitConverter.ToInt32(lengthBytes, 0);
    var buf = new byte[length];
    await this.stream.ReadAsync(buf, 0, buf.Length);
    return buf;

    Hi,
    Have you debug these two applications? Does it go into the catch exception block when you close the client or the server?
    According to my test, it will throw an exception when the client or the server is closed, just log the exception message in the catch block and then you'll get it:
    private async void Process()
    try
    while (true)
    var data = await this.Receive();
    this.NewMessage.Invoke(Encoding.ASCII.GetString(data));
    catch (Exception exception)
    Console.WriteLine(exception.Message);
    Unable to read data from the transport connection: An existing   connection was forcibly closed by the remote host.
    By the way, I don't know what the SafeInvoke method is, it may be an extension method, right? I used Invoke instead to test it.
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Handle Received data of Multiple TCP clients on TCP Server by displaying them into Datagrid

    Hello All,
    I have developed a C# based TCP server GUI application which is accepting the data from multiple TCP clients on TCP server.
    The data i am receiving from TCP clients is a 32 bit data. In my application multiple TCP client data goes like this:
    00012331100025123000124510321562
    01112563110002512456012451032125 and so on...
    Now i want those data of the TCP clients to be parsed into 4 bits first and display it in 8 columns (32/4=8) of (say) datagrid as each 4 bit represents some characteristics of the TCP client. The same thing
    should work for next TCP client on second row of datagrid.            
    Can you give me some suggestion or an example how to go about this? Any help would be appreciated.
     Thank you in advance.
    Here is my code for receiving data from multiple TCP clients.
    void m_Terminal_MessageRecived(Socket socket, byte[] buffer)
    string message = ConvertBytesToString(buffer, buffer.Length);
    PublishMessage(listMessages, string.Format("Sockets: {0}", message));
    // Send Echo
    // m_ServerTerminal.DistributeMessage(buffer);
    private string ConvertBytesToString(byte[] bytes, int iRx)
    char[] chars = new char[iRx + 1];
    System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
    d.GetChars(bytes, 0, iRx, chars, 0);
    string szData = new string(chars);
    return szData;

    Now i want those data of the TCP clients to be parsed into 4 bits first and display it in 8 columns (32/4=8) of (say) datagrid as each 4 bit represents some characteristics of the TCP client. The same thing
    should work for next TCP client on second row of datagrid
    If mean it's a Windows Forms application and you want to display those bits in a DataGridView control, then please see these threads:
    Add row to datagridview
    Programmatically add new row to DataGridView
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Stop tcp listen and tcp read

    Hi all,
    if i use the tcp listen and tcp read blocks with a time out of -1, how can i stop their execution if they are waiting for incoming connection or data?
    The tcp read block is in a while loop and the stop button state isn't evaluated, since the read block is in wait. 
    some one can tell me a solution to this problem compatible with S-RIO target device?
    thanks
    Solved!
    Go to Solution.

    i try to explain better with an example.
    1) run the server
    2) run the client
    3) try to stop the server without stopping the client --> the server never stops due to the infinite timeout on the reader.
    note: the client doesn't  close the connection after sending "hallo" 4 times.
    the question is: is it possible to have the server waiting for data and the stop but evaluated when pushed?
    Attachments:
    example.zip ‏15 KB

  • Difference between oracle client and oracle server?

    Hi,
    could any one please tell me the difference between these things?
    1.)oracle client and oracle server
    2.) installtion of oracle client and oracle server
    Thanks in advance.

    Take a look at the general client - server computing model, described e.g. in this wiki.
    edit:
    corrected bad link
    Message was edited by:
    orafad

  • Should i create a client and a server socket in a same application

    Greetings,
    In which situation sould i have a server socket ? and in which situation should i have a client socket ?and in which situation should i have both?
    I 'm making a app. who receives info (like alarms ) from a automation machine and also that same app. as a scheduler who send commands according to dates to the automation machine.
    Now the automation machine is programmed like a socket tcp/ip server who is always on and the app. is a client.
    Every time there's an alarm the machine sends me the info and i put it in a mysql database.
    Every time there's an event programmed the app. sends a string to the machine.
    The question is that i can't maintain that socket always connected, Sometimes disconnets.
    I was thinking of making the change of creating in both sides a server and a client, so that, for example, in the app. the client woul d handle the event msgs and the server would accecpt connections with alarms from the automaition machine.
    Since i'm a newbie in Java could somebody give me some tips, please?
    Thanks

    Thanks Peter....
    But i already do that....
    I have a thread who handles the connection management.
    If by some reaseon the connection is lost the thread reconnects it.
    My problem is that sometimes it reconnects every second, and i loose info provided by the automatian machine.
    The best thing to have it would be a socket listener, but Java does have any.
    Is there any API that does a socket listener?

  • Clarification about source and destination IPs for internal clients and Edge server

    I just wanted to get some clarification on the correct traffic flow between internal Lync clients and the Edge server.
    From all the diagrams I've looked at I was under the impression that if internal clients need to hit the Edge server to talk to external clients they should always do so through the Edge Internal interface which bridges to the Edge External interface and
    out to the internet.  Specifically port 3478 from the Edge AV External interface to the internal clients.
    We aren't seeing that in our environment.  When internal clients are talking to external clients we see the Edge AV External interface communicating directly with the internal client.  In fact we found this out because after the migration to Lync
    2013 external users couldn't created a AV connection to internal users on either the Lync servers.  We saw traffic on 3478 being dropped between the Edge AV External interface and the internal client.  Once we opened that port AV traffic worked.
    We never put this rule in until we introduced Lync 2013.  Lync 2010 didn't seem to require it.
    Is that the correct flow?

    I would also really love to know the outcome of this but it looks like the thread is marked as "Answered" and it is not so. 
    I've been working with a troublesome Lync deployment in which internal users are having issues sharing their desktop with external and federated users. After opening up all the 50000-59999 range for TCP/UDP on the A/V Edge external interface things are working
    much better, but we still see sporadic failures.
    It lead us to start digging into the network traffic. We see that UDP traffic on port 3478 is being routed back from the external client to the Edge A/V's external interface, inside of the DMZ's perimeter, then directly to the internal client on the internal
    network. It doesn't look like it's making a connection since the stream is so small, so I wonder if there is a design flaw in my topology?
    There are persistent static routes on the Edge server that use the internal interface to route internally directed traffic over the internal gateway. Tracert confirms the flow, but in wireshark traces, running during successful connections, UDP port 3478
    is still sending packets directly to the internal IP from Edge's A/V address. 
    We also see successfully connected sessions communicate on a different network route that we use to handle internet traffic rather than our Lync topology's route (the one defined for A/V traffic). The connection opens on ports in the 50000 range, but goes
    over a router that we have not configured for such traffic. Is that possible?
    Why is UDP traffic on 3478 trying to go directly to internal clients from external interface ?
    It sounds like it's happening elsewhere... Is this a legitimate issue to be diagnosing? Has it been observed and/or resolved by others?

  • Error in the Socket Communication between Java Client and VC++ Server

    In my application, using Java Client to do socket bi-communication with VC++ Server, which is done by somebody else.
    The error is after the application properly running one or two days, the VC++ Server cannot receive the messages passed by java Client, but at Java client, everything is the same, although using CheckError() after every print(), there is no exception thrown.
    The JVM is jdk1.3.1, platform is Win2k Server.
    The outputstream is PrintWriter().
    Please help me to settle down this problem. Thanks in advance.

    I read some thread in the forum, and found somebody had the similar problem with me. Just want to know how to settle this problem.
    In the client/server program. Client is a JAVA program and Server a
    VC++ program. The connection works, and the problem appears after some time. The Client sends a lots of requests to Serverm, the server seems receive nothing. But at the same time, the server is able to send messages to Client. The Client also can get the messages and handle them. Don't understand why there this problem and why it appears when it wants.
    The client is a Win2k platorm with JDK1.3.1 and the server is also a Win2K platform with VC++ 6.0.
    In the Client, using:
    inputFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    outputToServer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
    Hope can get your help.

  • Can i run UDP  client and UDP  server socket program in the same pc ?

    hi all.
    when i execute my UDP client socket program and UDP server socket program in the same pc ,
    It's will shown the error msg :
    "Address already in use: Cannot bind"
    but if i run UDP client socket program in the remote pc and UDP server socket program run in local pc , it's will success.
    anybody know what's going on ?
    any help will be appreciated !

    bobby92 wrote:
    i have use a specified port for UDP server side , and for client define the server port "DatagramSocket clientSocket= new DatagramSocket(Server_PORT);"Why? The port you provide here is not the target port. It's the local port you listen on. That's only necessary when you want other hosts to connect to you (i.e. when you're acting as a server).
    The server should be using that constructor, the client should not be specifying a port.
    so when i start the udp server code to listen in local pc , then when i start UDP client code in local pc ,i will get the error "Address already in use: Cannot bind"Because your client tries to bind to the same port that the server already bound to.

  • Socket Communication between java client and c++ server

    HI,
    In my project,I want to do the following:
    1.Sending datas from client to server.
    2.Getting the response from server to client.
    I written the client in java.but the server is in c++.
    Is it possible to communicate with the server using java codings itself?
    Im able to send the data from my java client to the server.
    but unable to get back the datas from server to client.
    Can anyone tell me how to do this?
    thanks a lot

    hi
    thanks for ur reply.
    I didnt get any error msg while getting the back the datas.
    Actually i divided my application into two parts.
    My application will act as both server and client.
    server ll get the browser request and send to the client and the client will send that data to the c++ server.
    Im able to do that.and unable to get the data from server.
    Didnt get any error.
    can u tell me how to make an application to act as both client and server.
    I think im wrong in that part.
    thanks a lot

  • NFS problem between RedHat Client and Solaris Server

    Hi all, we are experiencing a problem between a RedHat client and a Solaris 10 server. For the purposes of this post, I'll call the Redhat client server A and the Solaris 10 server B.
    Server B is exporting a filesystem that server A is trying to mount. Server A can successfully mount the exported file system, however, strange things are happening. If I change to the exported mount point on server A and create a file, the file is owned by nobody:nobody, not the user that created the file.
    A look at the file on server B shows the file has the correct UID and GID (ie the UID & GID of server A).
    The fstab file on server A looks like this:
    serverB:/data /data nfs4 rsize=32768,wsize=32768,hard,nointr,rw,bg,actimeo=0,timeo=300,suid 0 0
    Does anyone have a explanation for this?
    NB: There is a firewall between server A and server B. A firewall rule is in place to allow traffic between the two servers on port 2049
    Stewart

    Hi
    If I change to the exported mount point on server A and create a file, the file is owned by nobody:nobody, not the user that created the file.On a NFS share, for security reasons, you normally dont have root provileges.
    A file createt as root user will be mapped to nobody:nobody.
    The behaviour you see is correct.
    If you want the file to be createt as root, you have to export the filesystem with -o ro,anon=0
    NFSv3 will be blocked by your firewall.
    Franco

  • EAP with Windows 2000 client and IAS server

    Several messages on this site point to peole using EAP on a Windows 2000 client and authenticating against an IAS server. I am running an Aironet 350 AP and trying to setup my Windows 2000 clients to use EAP only and authenticate against a Windows 2000 AD forest via IAS. The access point and client are on the latest firmware and drivers (12.0 for AP). I have two basic questions.
    1. It is my understanding that by enabling Network-EAP as the only authenticaiton type that users will authenticate and then dynamic WEP keys will be used, greatly reducing the risks of compromised WEP keys while at the same time keeping the data encrypted.
    2. Does anyone have a quick HOW-TO or point-by-point list of how to configure the Windows 2000 client to authentication using the Network-EAP method? I am currently running into a situation where no matter what I configure on the client, the IAS server reports and error with "Reason: The authentication type is not supported on this system." I also noticed that the "Authentication-Type" and "EAP-Type" fields shown in the IAS messages in the Windows 2000 Event Viewer log have the value "<undetermined>". Has anyone else run into this?

    I'm having a similar problem. I'm trying to do PEAP and it appears that IAS is not handling the request properly. It keeps trying to log the user PEAP-##### in instead of setting up the TLS and then asking for Username, Pass, Domain. The IAS error message I'm getting is:
    User PEAP-00097CFCD901 was denied access.
    Fully-Qualified-User-Name = APPLY\PEAP-00097CFCD901
    NAS-IP-Address = 172.16.200.31
    NAS-Identifier = AP1
    Called-Station-Identifier = 004096570d87
    Calling-Station-Identifier = 00097cfcd901
    Client-Friendly-Name = WirelessAP
    Client-IP-Address = 172.16.200.31
    NAS-Port-Type = 19
    NAS-Port = 37
    Policy-Name =
    Authentication-Type = EAP
    EAP-Type =
    Reason-Code = 8
    Reason = The specified user does not exist.
    So if anybody has the needed settings for Win2k (SP3 and 802.1x patch) IAS it would be much appreciated.
    Ben
    Note: if I had PEAP-####### as a user in Win2k I get:
    User PEAP-00097CFCD901 was denied access.
    Fully-Qualified-User-Name = apply.org/Users/PEAP TEST
    NAS-IP-Address = 172.16.200.31
    NAS-Identifier = AP1
    Called-Station-Identifier = 004096570d87
    Calling-Station-Identifier = 00097cfcd901
    Client-Friendly-Name = WirelessAP
    Client-IP-Address = 172.16.200.31
    NAS-Port-Type = 19
    NAS-Port = 37
    Policy-Name = Wireless Policy
    Authentication-Type = EAP
    EAP-Type =
    Reason-Code = 16
    Reason = There was an authentication failure because of an unknown user name or a bad password.

  • Labwindows tcp client to generic server

    I made a program fom the client sample in the TCP sample folder. Now I want that program to communicate to a generic server that doesn't have labwindows running on it. Is this possible with just a few minor edits or do I need to start from scratch with a different sample program?
    Solved!
    Go to Solution.

    I see, but I can't understand why it reports no error! The macro exits for return value <0: if directly passed to GetTCPErrorString should not return a "no error" string, neither should GetTCPSystemErrorString do.
    Are you sure you are not calling any other TCP library function before decoding the error? If this other function succeeds, it masks out the previous error.
    Proud to use LW/CVI from 3.1 on.
    My contributions to the Developer Zone Community
    If I have helped you, why not giving me a kudos?

  • VPN Client and Terminal Server

    We have several clients that allow us to vpn into their systems and it has come to the point that we are getting software incompatabilities. What I am trying to do is set up "compatable" connections on a Terminal server box and let our people access this from their workstations. The problem is when the acuall connection thru the vpn client is made they loose connection to the terminal server. I have tried putting in 2 nic's into the terminal server and am able to allocate one for the terminal server but can not find a way to allocate the other to the VPN CLient. Is this possable or is there another way to accomplish this?
    Thanks

    Close,
    What I have is one machine with 2 nic's
    NIC #1 = Terminal Server Access (local lan only) Locked in via registry settings to use ONLY this NIC
    NIC #2 = I would like to "LOCK" the Client software to use ONLY this nic (has a dynamic IP for local lan and access to the Inet via a router.
    Problem: When you connect to the T-Server all is fine UNTIL you start up the client software to access our clients systems via the web connection to the T-Server on the local side stops and gives the appearance of a frozen screen.
    manualy disconnect person from the T-Server and kill the cisco client software then you can re-connect to the T-Server (and it all starts over again)
    The Cisco software acualy makes the connection to our clients system but we can not tell because it want BOTH the NIC's for itself and stops access via NIC#1 to the T-Server.

  • Data transfer between C client and Java server

    Hello there
    I am working on a project where I have to develop a Client based on C and Server based on Java. The client can connect to Java server and it then sends a integer/string to Java server. But.. Java server unable to receive that and throws an IOException.
    I use write method to send the integer buffer to the socket.
    int out_buffer = 0;
    int *pbuf;
    pbuf = &out_buffer;
         if (write(acskfd, pbuf, 4)< 0){
                   syslog(LOG_ERR,"Write failed. %s(%d)", strerror(errno), errno);
                   printf("\tCLIENT:\tWrite failed\n");
                   exit(1);
    In Java, i use DatainputStream and readnInt method to read the integer from the stream.
    cl_sock = socket_out.accept();
    DataInputStream sInput =new DataInputStream(cl_sock.getInputStream()) ;
    int cmd = sInput.readInt();
    Am I missing someting.. Any suggestions would be really really helpful.
    Thanks
    Ithaca
    PS: I running both programs in the same machine.
    In C part, I also use host to network byte order conversion (serv_addr.sin_port = htons(portno).

    I would suggest writing a Java client to perform the same tasks as the C client.
    Then if the Java client does, or does not work this can help dteremine which end is at fault.
    Are you flushing your data from the client?

Maybe you are looking for