The Transport Protocol thread failed. java.io.IOException: The socket is EOF

Hi Friends,

When I was trying to SFTP file to the destination server by opening session using SSHConnect, I got “The Transport Protocol thread failed. java.io.IOException: The socket is EOF” error. When I tried to fix this issue, I’ve learnt from many websites and forums that, this error occurs when we are using j2ssh jar to open the session with destination server, the version of j2ssh does not support with the SSH version installed in destination server. To overcome this issue, we need to migrate from j2ssh jar file to jsch jar file which also supports to transfer the file securely using SFTP to the server.

I’ve consolidated the exception that occurred, features of jsch.jar file and code snippet to transfer the file using SSH and SFTP in this post.

Below is the “The Transport Protocol thread failed. java.io.IOException: The socket is EOF” error that occurred:

[CAR] [2013-12-30 05:10:32,360] ERROR [Transport protocol 1] TransportProtocolCommon.run(?) | The Transport Protocol thread failed
java.io.IOException: The socket is EOF
 at com.sshtools.j2ssh.transport.TransportProtocolInputStream.readBufferedData(Unknown Source)
 at com.sshtools.j2ssh.transport.TransportProtocolInputStream.readMessage(Unknown Source)
 at com.sshtools.j2ssh.transport.TransportProtocolCommon.readMessage(Unknown Source)
 at com.sshtools.j2ssh.transport.kex.DhGroup1Sha1.performClientExchange(Unknown Source)
 at com.sshtools.j2ssh.transport.TransportProtocolClient.performKeyExchange(Unknown Source)
 at com.sshtools.j2ssh.transport.TransportProtocolCommon.beginKeyExchange(Unknown Source)
 at com.sshtools.j2ssh.transport.TransportProtocolCommon.onMsgKexInit(Unknown Source)
 at com.sshtools.j2ssh.transport.TransportProtocolCommon.startBinaryPacketProtocol(Unknown Source)
 at com.sshtools.j2ssh.transport.TransportProtocolCommon.run(Unknown Source)
 at java.lang.Thread.run(Thread.java:662)
java.io.IOException: The socket is EOF
 at com.sshtools.j2ssh.transport.TransportProtocolInputStream.readBufferedData(Unknown Source)
 at com.sshtools.j2ssh.transport.TransportProtocolInputStream.readMessage(Unknown Source)
 at com.sshtools.j2ssh.transport.TransportProtocolCommon.readMessage(Unknown Source)

The error “The  Transport Protocol thread failed. java.io.IOException: The socket is EOF” occurs when j2ssh.jar file is not compatible with current SSH version of SFTP server.

Suppose, if we are trying to SFTP the file to a Solaris Server which has following SSH version and our Java class uses j2ssh.jar with version 0.2.9, then we will get “The  Transport Protocol thread failed. java.io.IOException: The socket is EOF”.

j2ssh.0.2.9.jar

j2ssh.0.2.9.jar does not support latest SSH version installed in Solaris machine

Below is the SSH version installed in Sun Solaris destination server

afusyj7@belkvmhs63:/www/qa/integrationdata/final/vendorinfo/in$ ssh -V
Sun_SSH_2.2, SSH protocols 1.5/2.0, OpenSSL 0x100000bf

 To overcome “The  Transport Protocol thread failed. java.io.IOException: The socket is EOF” error replace the jar file j2ssh.0.2.9.jar with jsch.jar file.

You can find the features of jsch.jar from here : http://www.jcraft.com/jsch/

Download jsch.jar, http://sourceforge.net/projects/jsch/files/jsch.jar/0.1.50/jsch-0.1.50.jar/download

Following is the method to create a SFTP connection with the destination server using jsch API:

private static final String HOST_KEY_CHECKING = "StrictHostKeyChecking";

public static Session openSftpSession(String host, String username,String password, int port, String hostKey) {
	// Connect and logon to SFTP Server
	JSch jsch = new JSch();
	Session session = null;
	try {
		session = jsch.getSession(username, host, port);
		session.setPassword(password);
		session.setConfig(HOST_KEY_CHECKING, hostKey);
		session.connect();
		// check if connect was successful
		if (session.isConnected()) {
			System.out.println("Connected sucessfully to server :" + host);
		} else {
			System.out.println("Connection Failed" + host);
		}
	} catch (JSchException e) {
		System.out.println("Connection Failed" + host + " Error:" + e.getMessage());
		e.printStackTrace();
	} catch (Exception e) {
		System.out.println("Connection Failed" + host + " Error:" + e.getMessage());
		e.printStackTrace();
	}
	return session;
}

Following is the method which transfers file to SFTP server using jsch API:

public static void sendDataFiles(ChannelSftp channelSftp,Collection<File> files, String destinationFolder) {
	if (channelSftp == null) {
		System.out.println("SFtp Connection is not established or invalid.  Please call openConnection before calling this method!!");
		return;
	}
	if (files == null) {
		System.out.println("No files to send!!!"); 
		return;
	}
	System.out.println("Destination Folder: " + destinationFolder);
	List<File> filesToProcess = new ArrayList<File>(files);
	InputStream iputFile = null;
	for (File file : filesToProcess) {
		try {
			if (file.isDirectory()) {
				filesToProcess.addAll(Arrays.asList(file.listFiles()));
				System.out.println("Directory: " + file.getName() + " filesToProcess.size(): " + filesToProcess.size());
				continue;
			}
			iputFile = new FileInputStream(file);
			try {
				channelSftp.put(iputFile, destinationFolder+file.getName());
			} catch (Exception ex) {
				System.out.println("SFTP:  FAILED for file " + file.getName());
				ex.printStackTrace();
			}
			System.out.println("SFTP:  SUCCESS for file: " + file.getName());
		} catch (FileNotFoundException ex) {
			System.out.println("SFTP:  FAILED for file " + file.getName());
			ex.printStackTrace();
		} finally {
			try {
				if (iputFile != null)
					iputFile.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}

Following is the method to close SFTP connection:

public static void closeSftpConnection(Session session, ChannelSftp sftpChannel) {
	try {
		sftpChannel.disconnect();
		session.disconnect();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

Following is the code snippet which calls above methods to create connection, transfer files and close the connection using jsch API:

String ftpHost = "10.1.52.3";
String ftpUserId = "admin";
String ftpPassword = "admin";
String ftpRemoteDir = "/techydiary/docs/";
File file = new File("/local/docs/ioException.txt");

Session session = SFtpUtil.openSftpSession(ftpHost,ftpUserId ,ftpPassword, 22, "no");
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp  sftpChannel = (ChannelSftp) channel;
SFtpUtil.sendDataFiles(sftpChannel, Arrays.asList(file), ftpRemoteDir);
SFtpUtil.closeSftpConnection(session,sftpChannel);

In the above code, first we create the session with SFTP server and transfer the data files once the username and password matches and authenticated. The session has to be closed once the transaction between the servers are completed.

This will help to release the connection between two servers and to avoid future exceptions when the same server tries to open the session with the destination server.

Share This Post

Related Articles

Leave a Reply

© 2017 Techy Diary. All rights reserved.
Powered by Charvi Groups